05-30-2017 07:14 PM
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.
05-30-2017 08:13 PM
One place to learn some RegEx is the Regular Expression Board (https://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/1187799#M13459).
05-31-2017 12:35 AM
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.
05-31-2017 12:57 AM
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.
05-31-2017 01:55 AM
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).
05-31-2017 09:26 AM - edited 05-31-2017 09:26 AM
Here is a tutorial that might be of interest to you.
06-01-2017 11:08 AM
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!
06-01-2017 11:09 AM
I was thinking there should be tutorial, but I hadn't found one. Thanks.
06-01-2017 12:25 PM
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.