LabVIEW

cancel
Showing results for
Search instead for
Did you mean:

Solved!
Go to solution

Match Regular Expression with 2 expressions

I'm trying to create an expression that will search a string for 1 of 2 patterns using the Match Regular Expression function in LabVIEW 2016. I want to pick out 12 characters from a string of some length in a line.

The patterns are as follows, where 'L' is a letter (A-Z) & 'N' is a number (0-9):

1.        NNNNLNNNNNNN
2.        LNNLLNNNNNNN

Is it possible to create an expression to do one of two things?

1. Search for either pattern with an OR statement
2. Search for the first four characters with an OR statement & the last eight with an AND; ie. (NNNN | LNNL) && LNNNNNNN

Examples of valid match:

1. 0123A9876543
2. A12AB9876543
Message 1 of 8
(4,089 Views)

Re: Match Regular Expression with 2 expressions

For the first part, try this:

[0-9]{4,4}[A-Z][0-9]{7,7}|[A-Z][0-9]{2,2}[A-Z]{2,2}[0-9]{7,7}

To do it the second way:

([0-9]{4,4}|[A-Z][0-9]{2,2}[A-Z])[A-Z][0-9]{7,7}

Message 2 of 8
(4,043 Views)

Re: Match Regular Expression with 2 expressions

Not an answer but I've found using the following website to be very helpful when working on any regular expressions.

https://regexr.com/

Matt J | National Instruments | CLA
Message 3 of 8
(4,038 Views)

Re: Match Regular Expression with 2 expressions

Your second regex that doesn't work will work if you remove the spaces right before and after the pipe character "|".

Ben64

Message 4 of 8
(4,033 Views)
Solution
Accepted by SgtPeprLHCB

Re: Match Regular Expression with 2 expressions

([0-9]{4}[A-Z]|[A-Z][0-9]{2}[A-Z]{2})[0-9]{7}

Message 5 of 8
(4,031 Views)

Re: Match Regular Expression with 2 expressions

Thank you all! All great answers and all work. I went with the last answer since it was the shortest.

Kudos to Matt J. for the great webpage.

Message 6 of 8
(4,003 Views)

Re: Match Regular Expression with 2 expressions

You can still replace [0-9] with \d to make it shorter!

Ben64

Message 7 of 8
(3,996 Views)

Re: Match Regular Expression with 2 expressions

Besides the \d:

45 characters: ([0-9]{4}[A-Z]|[A-Z][0-9]{2}[A-Z]{2})[0-9]{7} ->

36 characters: (\d{4}[A-Z]|[A-Z]\d{2}[A-Z]{2})\d{7}

the last [A-Z] in the first capturing group can be brought outside the OR:

32 characters: (\d{4}|[A-Z]\d\d[A-Z])[A-Z]\d{7}

Obviously, less characters should not be a goal, but I think it's a bit clearer as well...

Message 8 of 8
(3,954 Views)