From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Allow specific characters - Regular Expression

Solved!
Go to solution

Hello everyone

 

I am new to regular expression and I have a very simple question. I use the "read from text file" function to load a Tab delimited file with 3 columns into my VI. Next, the string is converted in array and I use the values.

 

Nevertheless, I want to develop a "filter" allowing only digits (0-9), colon, comma and point into strings.

 

Using the "match regular expression" function, I was trying a regular expression like that:

 

[^0-9]|[^\].[|^:]|[^,]

 

But it is not working.

 

Could someone help me with this issue?

 

Thanks

 

Dan07

 

 

0 Kudos
Message 1 of 11
(3,990 Views)
Solution
Accepted by topic author dan07

Use the Search and Replace String function with Regular Expression selected.

 

Example_VI.png

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 2 of 11
(3,965 Views)

Here's an alternate method using Match Regular Expression, but a wise man once said "When I find myself facing a regex in a loop, I try to see if my old friend Search and Replace can help."

 

forum Regex.png

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 11
(3,958 Views)

Hello

 

Actually I don't need to modify the string that has "invalid" characters, I just need to identify them instead. Find below a VI testing both methods: Match Regular Expression and Search and Replace String.

 

Using Match Regular Expression method, I got correct results since all the "valid" values must be identified as "-1" and all the "invalid" values must be identified as positive numbers (offset).

 

Nevertheless, using Search and Replace String method I got wrong results, since all the strings were classified as "valid" (-1), but "bg" and "03/12/2010" are not "valid".

 

I will go ahead with Match Regular Expression method because it is working great, but I was just wondering how to fix Search and Replace String method to achieve equivalent results.

 

Thanks

 

Dan07

 

BD.jpg

FP.jpg

0 Kudos
Message 4 of 11
(3,920 Views)

Look closely at Jim's Search and Replace Node then look at yours.  Notice the missing '*' that means that Regular Expressions are not enabled.  Right Click and Select Regular Expression, then try it again.

Message 5 of 11
(3,916 Views)

Darin K.

 

You are right about that. I changed it to "Regular Expression" and worked great (attached image). It's interesting to see that Match Regular Expression represents the location of the found string starting at index 1, and Search and Replace String starting at index 0.

 

Thanks

 

Dan07

 

 

FP2.jpg

0 Kudos
Message 6 of 11
(3,902 Views)

@dan07 wrote:

 

It's interesting to see that Match Regular Expression represents the location of the found string starting at index 1, and Search and Replace String starting at index 0.


They're both using the same index (starting at zero), but they're looking at different things.  The offset past replacement from the Search and Replace String function is giving the position in the result string.  The offset past match in the Match Regular Expression function is giving the position of the match in the input string.

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 11
(3,831 Views)

jcardmody,

 

Couple months ago you helped me with a code to check if the string has just valid characters: digits (0-9), coloncomma and point

 

 

Why did you use [^0-9:,\.] ?

 

Is [^0-9:,.] the same thing?

 

Why did you use the \ ?

 

Thanks

 

Dan07 

0 Kudos
Message 8 of 11
(3,666 Views)

Escaping the '.' is probably just a habit that Jim has gotten into.  Outside of an alternative '.' will match any character except newline, so it is easy to get confusing results when actually trying to match a '.' unless you remember to either enclose it in an alternative ([.]) or escape it (\.).  Escaping it inside the alternative is unnecessary yet harmless. 

0 Kudos
Message 9 of 11
(3,654 Views)

Darin

 

So, the best option for me would be  [^0-9:,][.] ??

 

Thanks.

 

Dan07.

0 Kudos
Message 10 of 11
(3,649 Views)