Match Regular Expressions Function Returns Incorrect Match

Updated Aug 1, 2018

Reported In

Software

  • LabVIEW

Issue Details

I am using the Match Regular Expression function in LabVIEW to locate a regular expression string in a larger string. The strings are displayed in hexademical format. The regular expression string returns as a match in the larger string when the two are not equal and do not match! What is happening?

Solution

The Match Regular Expression function utilizes special characters to create regular expressions (also called regex) to describe a search pattern. In the Match Regular Expression Function, the regular expression input defines your search string for the given input string.

If your search string contains special characters used by regular expressions (regardless of display format), your function will return values based on the regular expressions search parameters. For example, if you are using the regular expression string "abc." to find a match in the input string "abcdefg" the returned values will be:
  • before substring: null
  • match substring: abcd
  • after substring: efg
  • offset past match: 4
The reason the match string returns "abcd" and not null is due to the special character of "." being included in the regular expression. The "." special character is interpreted as a wildcard in regular expressions and will return almost any value that it see (in this case the character "d" in the input string). 

To avoid a special character from being interpreted as a regular expression, you can place the "\" character before the special character to "escape" its function. For example, if you are using the regular expression string "abc\." to find a match in the input string "abcdefg" the returned values will be:
  • before substring: abcdefg
  • match substring: null
  • after substring: null
  • offset past match: -1
which is correctly interpreting the regular expression as not being a match to the input string.

Additional Information

If your function does not explicitly need to utilize regular expression, other string matching tools, like Search/Split String, would be more appropriate.

WAS THIS ARTICLE HELPFUL?

Not Helpful