BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular Expressions Board

Here is a basic XML parser that will work regardless of whether the tag has attributes or not. It will also return the list of attributes. The offset is used if you need to retrieve multiple items with the same tag.

 

XML parser.png



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 71 of 150
(7,985 Views)

@IziShaw wrote:

[...] I was trying something like \(Ndx\s[0-9]+\) and so on. I was getting the whole match

 


Do you understand the regular expression?  I looked for a literal '(', then used submatches to get the data name and value.  I frequently use this type of construct: "[^ ]* ".  It looks for "a string of anything that's not a [something], followed by a [something].  In this case, it's looking for the opening "(" and making a submatch of everything up to the first space, then a submatch of everything up to the ")".

 

\(([^ ]*) ([^\)]*)\)

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 72 of 150
(7,980 Views)

I was wondering if someone could help me with this "simple" regex problem.

 

We have a template naming convention as follows:

<code>_<version_number>_<name>.mtt   eg.  IDS1_v1_IDS+PSU_Procedure ver2.mtt

 

and the <code> segment must follow these rules:

1) Must have only two or three captial letters followed by

2) only one or two digits

 

Now I'm trying to create an expression that will confirm if this is occuring as follows

Regex example.png

ie. look for 2 or 3 capital lettes [A-Z]{2,3} followed by 1 or 2 numbers [0-9]{1,2}

 

Which works for most situations, but as you can see not the one above, or something like CS5+

Can anyone help?

Chris

0 Kudos
Message 73 of 150
(7,895 Views)

You're almost there! If you're trying to validate only the first prefix in the protocol, try the regex [A-Z]{2,3}[0-9]{1,2}$ -- the dollar sign anchors to the end of the string, and will not validate your two test inputs of CS300 or CS5+

0 Kudos
Message 74 of 150
(7,889 Views)

It's OK I figured it out - I wasn't anchoring the beginning and end of the string

 

^[A-Z]{2,3}[0-9]{1,2}$

 

Chris

0 Kudos
Message 75 of 150
(7,886 Views)

Thanks Jack for your answer - now I was wondering if there was a more elegant solution for the <name>.mtt portion than what I've done below?

 

Basically I want to parse anything in the <name> portion (underscores, periods, hyphens etc), but filter off just the .mtt characters.

 

eg.   IDS1_v1_IDS+PSU_Procedure.mtt should give me IDS+PSU_Procedure

 

Regex example2.png

 

 

Can you go two for two Jack?

0 Kudos
Message 76 of 150
(7,964 Views)

@ChrisReed wrote:

Thanks Jack for your answer - now I was wondering if there was a more elegant solution for the <name>.mtt portion than what I've done below?

 

Basically I want to parse anything in the <name> portion (underscores, periods, hyphens etc), but filter off just the .mtt characters.

 

eg.   IDS1_v1_IDS+PSU_Procedure.mtt should give me IDS+PSU_Procedure


How about something like the regex ^([A-Z]{2,3}[0-9]{1,2})_(.+?)_(.+?)\.mtt$ and dropping "Scan from String" in favor of "Match Regular Expression". Don't forget to escape the period before the file extension \.mtt rather than just .mtt (which would mean, any char followed by mtt) - and also check out laziness that the ? introduces to the .+ "match one or more characters" pattern. (And the parentheses in LabVIEW are treated as submatches)

 

Filename-Validation-Regex

Message 77 of 150
(7,956 Views)

Oh so that's how you can get submatches with the Match Regular Expression VI Smiley Surprised

 

I threw all manner of rubbish template file names at your Regex and they all passed with flying colours - thanks

 

Chris

0 Kudos
Message 78 of 150
(7,950 Views)

I thought this board was intended to post examples of regex, and not to answer questions. Like the Excel board. I'm confused.

0 Kudos
Message 79 of 150
(7,941 Views)

But then regular expressions are pure gibberish to me anyway, so it's not that big a of surprise. I get a headache every time I look at them or have to use them.

0 Kudos
Message 80 of 150
(7,931 Views)