10-04-2011 07:18 PM
Hello,
I ran into the following problem: I have a string returned from an instrument through the serial interface. I would like to extract the numerical value from the string (double precision format). If I use the scan from string function, it does not find the numerical value. Similarly, if I use the Match Pattern function and go looking for the character "[", it does not find it returning offset past match of -1. What is going on?
Note that I cannot expect that the instrument always returns 9 characters before the start of the number.
Thanks for your help.
Peter
(using LabVIEW 8.5 on a WinXP machine)
Solved! Go to Solution.
10-04-2011 07:39 PM - edited 10-04-2011 07:45 PM
Scan from string assumes what you scanning for starts at the initial scan location. And since your string doesn't start with a number (or white space followed by a number) it doesn't find anything. The '[' character has special meaning for the match function so you have to escape it (use '\['). If you escape the '[' character and feed the offset past match into the initial scan location, then the scan from string will read the number.
10-05-2011 05:10 AM
A Regular Expression will find your number.
-? will look for an optional minus sign
\d* looks for zero or more digits
\.? looks for an optional decimal point (the \ escapes the special 'dot' character)
\d+ gets any digits past the decimal point
10-05-2011 05:18 AM
10-05-2011 05:31 AM
Interesting that one Jim,
I think it must be that everything in your regex is optional if you use \d* in both places.
If we add something that is fixed, like changing the \.? to \. it will match the .
So I suspect that we just must have something concrete in the regex for it to work at all
With a numeric, that is no problem anyway as we would always have a digit somewhere.
In your example I guess it doesnt matter which \d is * and which is +
For the original poster, you could expand the regex to include all floting point numbers by searching for regex [+-]?\d+[.]?\d*+(?:[eE][+-]?\d+)?
Regards,
Steve
10-05-2011 06:47 AM
Try this:
10-05-2011 07:25 AM
10-05-2011 08:06 AM
Alright, here's the snippet
10-05-2011 04:37 PM
Thank you all for offering several different solutions to my issue. All of them work fine. Thanks again.
10-06-2011 12:58 PM
@Stephen C wrote:
For the original poster, you could expand the regex to include all floting point numbers by searching for regex [+-]?\d+[.]?\d*+(?:[eE][+-]?\d+)?
Regards,
Steve
This regex can be simplified to [Ee+-\d.]+
Ben64