This widget could not be displayed.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Scan from string does not find number

Solved!
Go to solution

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)

0 Kudos
Message 1 of 14
(8,383 Views)

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.

0 Kudos
Message 2 of 14
(8,379 Views)

A Regular Expression will find your number.

 

String%20Test[1].png

 

-? 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

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 3 of 14
(8,353 Views)

I'd sure appreciate if someone would tell me why \d* doesn't work where \d+ does.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 4 of 14
(8,350 Views)

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

Stephen C
Applications Engineer
Message 5 of 14
(8,346 Views)
Solution
Accepted by topic author pbuerki

Try this:

 

Message 6 of 14
(8,330 Views)

yuno.PNG

 

I tried to drag-n-drop it onto a BD.  Kudos, anyway.  🙂

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 7 of 14
(8,314 Views)

Alright, here's the snippet

 

Snippet Test.png

0 Kudos
Message 8 of 14
(8,303 Views)

Thank you all for offering several different solutions to my issue. All of them work fine. Thanks again.

0 Kudos
Message 9 of 14
(8,259 Views)

@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

Message 10 of 14
(8,231 Views)