LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular expression using submatch

I am looking at code written by a previous engineer and trying to understand how regular expressions were used.  I would like to learn how to expand it as well.  Is there a good example that explains how to do regex in LabVIEW?

 

My existing vi outputs DC, 1M and 1 in the three substrings with an input of "DC, 1x, 1M".  I would like to add "2V, -5Vo" and have substrings of 2 and -5.

0 Kudos
Message 1 of 9
(6,105 Views)

One place to learn some RegEx is the Regular Expression Board (https://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/1187799#M13459).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 9
(6,082 Views)

With regular expressions (and timestamps!) I usually have to open the LabVIEW help pages. Right clicking on the RegExp node and selecting help will open a page describing the function, but scrolling down a little you can find a link to regular expressions. If you want to instead search the help, then the page is titled 'Special Characters for Match Regular Expression and Search and Replace String' in my Help page version.


GCentral
Message 3 of 9
(6,055 Views)

The help wasn't very helpful which is why I'm here. It's like learning how to speak a language by looking at a dictionary.

0 Kudos
Message 4 of 9
(6,051 Views)

Although I can see the analogy, and agree it is sort-of true, at least it's not a very long dictionary!

 

In your example VI, you can see sets of parentheses enclosing sets of other characters. These parentheses enclose the outputs of the substrings. To add more outputs, drag the block down and add more parentheses. Filling the parentheses requires knowing what sets of inputs you want to consider - you've listed 2V and -5Vo, but what are other possibilities?

 

For example, is the first block always a single digit, followed by a V, or a set of numeric digits followed by a V, or can other inputs be possible? Can the input be ignored (like in the current input, which doesn't have a ...V part to the string)? What do you want to get if that is the case? Should lower-case 'v' also be accepted?

 

In the first pair of parentheses, you can see that it matches any of 'AC', 'DC', 'aC', 'dC', 'Ac', 'Dc', 'ac' or 'dc', but nothing else. It contains two 'character classes' with no repeating markers like *, ? or + (any number of repeats including 0, 0 or 1, 1 or more). These character classes are [AaDd], which will match one character from that list, and [Cc], which does the same, hence the 8 possible outcomes (or rather, valid inputs).

 

To match a single digit and a V, you'd probably want something like '[1-9]V' (excluding the 0 option), but for multiple digits you might choose to use '[1-9][0-9]*V' which allows any integer value followed by an upper-case V, and forbidding leading zeros. If you want to allow lower case 'v', then replace 'V' with '[Vv]', as in the DC/AC case.

 

The '-' character should be specified outside of a character class with an escaping '\', because otherwise it indicates a range (like [0-9] giving all digits between 0 and 9).

 

regexp.png


GCentral
Message 5 of 9
(6,045 Views)

Here is a tutorial that might be of interest to you.

Now Using LabVIEW 2019SP1 and TestStand 2019
Message 6 of 9
(6,022 Views)

Awesome thanks.  These are scope parameters that get passed in from an ini file.  The V value is the volts/division and Vo is the voltage offset.  So Vo will need to be positive and negative values and one or two digits.  I should be able to figure it out from here.  Thanks!

0 Kudos
Message 7 of 9
(5,989 Views)

I was thinking there should be tutorial, but I hadn't found one.  Thanks.

0 Kudos
Message 8 of 9
(5,987 Views)

You might find http://www.regexr.com/ useful for testing your expressions and building them quickly, will also help you figure out what your sub-expressions should be.

Jon D
Certified LabVIEW Developer.
Message 9 of 9
(5,971 Views)