LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

string comparison with wildcards

We have a test system where a Labview VI reads in an excel file as a script and executes it as a series of steps to carry out a given test.  A script may contain a wide variety of commands as well as parameters with which to execute said commands.  This system has significant legacy use across multiple sites.

 

One particular command includes the following steps:

 

1) Send out a string of data obtained from the script via a COM port.

2) Receive data back from the COM port as a string.

3) Compare the COM port received data string against a given 'expected' data string supplied in the script.

 

Right now the compare step above is implemented by passing the received COM port data string and the script expected data string to an Equal? function to get a T/F result.

 

I've arrived at a problem where it would be beneficial to be able to use a 'wildcard' in the script when specifying the 'expected' data string so that certain character positions in the string always return a true result.

 

For example (using % as the wilcard):

 

expected string: "81,%%,56,4F"

returned string: "81,11,56,4F" | result: T

compare string: "87,DB,56,4F" | result: F

 

expected string: "output: 5.2% volts"

returned string: "output: 5.20 volts" | result: T

returned string: "output: 5.19 volts" | result: F

 

The Equal? function doesn't appear to have provision for this sort of wildcard operation.  Is there another Labview function that's an approximate a drop-in replacement for the Equal? function but can be used to add the above functionality?  Thanks.

0 Kudos
Message 1 of 8
(3,156 Views)

The string palette has a rich collection of function to match string patterns.

 

You could also convert the string to a byte array and multiply it with an array os same length containing either 1 (to match) or zero (for ignore), then do an equal comparison with another array that has zeroes in the ignored places.

0 Kudos
Message 2 of 8
(3,137 Views)

Check out Match Pattern and Match Regular Expression. You could also try Scan From String to pull numeric values out of the string, which might be more like what you're looking for in your second example.

0 Kudos
Message 3 of 8
(3,125 Views)

I did look through the string palette, but didn't find anything that seemed straightforward.

 

My thinking right now is to replace each wildcard in the expected string with the corresponding character of the returned string and then compare.

 

My approach is to pass the expected string to a "Search And Replace" inside of a while loop that iterates over the length of the returned string.  Then use a "String Subset" with the returned string as an input and index it using the offset output from the "Search and Replace", to pass the replace characters back to the "Search and Replace" function.

 

I think I'll need to nest a loop to get the first index location loaded into the "String Subset" correctly.  Never built anything new in Labview before, so any suggestions would be appreciated.

0 Kudos
Message 4 of 8
(3,124 Views)

Forget your own custom wildcard system.  Place regular expressions in your spreadsheet.  Then as BertMcMahan said, use "Match Pattern" or "Match Regular Expression".  It's quite straightforward.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 5 of 8
(3,101 Views)

Try using Match Pattern. You will need to change your wildcard characters to "." (which is the "Any character" specifier for a regex) and you will need to escape your match string. This should work:

 

search.png

 

 

 

 

0 Kudos
Message 6 of 8
(3,097 Views)

Thanks all for the input.  I'm building both my own system as well as the approach suggested above, mostly as a learning activity.

 

With respect to the image above, I can't seem to find the left-most function used for escaping the string in my version (2011) of Labview, could someone identify it?  Additionally, the version of "Match Pattern" I have available differs from the one above in that the [b+] input is instead labelled as [b*], does this make a difference?  

0 Kudos
Message 7 of 8
(3,016 Views)

Sorry about that, I didn't realize you were that far back. That VI is called "Escape string" and it's included in my LabVIEW, but it's not very complicated. I replaced it with the subVI contents and backsaved to 2011. It basically just loops through a string and replaces one character set with another character set. You have to do this to use your specially formatted strings in Match Pattern. For example, the "." character is interpreted by Match Pattern as "A single instance of any character at all." If your formatting string has a . in it, Match Pattern won't treat it as a literal period, it'll treat it as a wildcard character. To get Match Pattern to recognize it as a period, you have to add a \ before the period. The Escape string stuff just adds a \ before any of the special formatting characters, as you're only interested in the % sign being a wildcard.

 

Basically, you take your string with a % as wildcard, escape out all of the other potential "wildcard" style characters, then replace all of the % signs with a . (since Match Pattern uses a period as a wildcard, not a % sign), then run it through Match Pattern.

 

search2.png

 

Check out the Help for Match Pattern for what all of the different escaped characters do. Some of them match 0 or more characters, some match 1 or more of a specific character, etc. It's very flexible- moreso than you need for this application, hence making sure they're not in the specifier string.

0 Kudos
Message 8 of 8
(2,981 Views)