LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Match Regular Expression with 2 expressions

Solved!
Go to solution

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
0 Kudos
Message 1 of 8
(4,200 Views)

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,154 Views)

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,149 Views)

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

 

Ben64

0 Kudos
Message 4 of 8
(4,144 Views)
Solution
Accepted by topic author SgtPeprLHCB

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

Message 5 of 8
(4,142 Views)

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.

0 Kudos
Message 6 of 8
(4,114 Views)

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

 

Ben64

Message 7 of 8
(4,107 Views)

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
(4,065 Views)