From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

Regexp: how to get random string matching to specific RegExp?

Solved!
Go to solution

I work on labview application which needs to scan various bar-codes and match them to regular expressions from database. For testing purpose I would like to solve opposite task - be able to generate some random strings which are matching to my specific regular expressions. Strings are kind of long, up to 16 characters, various length, so dirty brute-force in while-loop until match found does not look like a good idea. Are there any better ideas?

Below is a sample of my regular expressions: [0-9A-F]{8}[0-3C]{1}[0-9A-F]{4}A:[0-9A-F]{1}

0 Kudos
Message 1 of 3
(8,496 Views)
Solution
Accepted by topic author SergeS

Hi Serge,

 

[0-9A-F]{8}[0-3C]{1}[0-9A-F]{4}A:[0-9A-F]{1}

So you need an 8-digit hexadecimal formatted number, followed by one char of [0123C], followed by a 4-digit hex number, "A:" and at last a one-digit hex number?

 

Generate 4 random numbers, scale them as needed (U32, U16) and use FormatIntoString to generate your "barcode". (For [0123C] I would use an IndexArray function with an array constant containing those chars.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 3
(8,470 Views)
Solution
Accepted by topic author SergeS

wrote:

Below is a sample of my regular expressions: [0-9A-F]{8}[0-3C]{1}[0-9A-F]{4}A:[0-9A-F]{1}


There is no random length in this match. To match it needs:

 

exactly 8X (0-9 or A-F)

exactly 1X (0-3 or C), not 0 to 3C btw.

exactly 4X (0-9 or A-F)

exactly A:

exactly 1X (0-9 or A-F)

 

This could be simplified to: [0-9A-F]{8}[0-3C][0-9A-F]{4}A:[0-9A-F]

 

If those numbers are the minimum, you should use:  [0-9A-F]{8,}[0-3C]{1,}[0-9A-F]{4,}A:[0-9A-F]{1,}.

 

There is not one technique that will generate this. You'll need a variety of code:

1) to generate a 0-0xF digit, multiply a random number with 15, convert to integer, convert to string.

2) to generate 8 or more of them, multiply a random number with the max, and add 8. Convert to integer and execute 1) in a for loop.

3) to generate 0 to 3 or C, make a string array with 0,1,2,3,C, and use a random number X 4 (not 5!) to index it.

4) to generate nX3), same as 2.

5) repeat 2 and 4, and concatenate the results.

 

Note that carefully chosen sub VI's will make your life a lot easier...

0 Kudos
Message 3 of 3
(8,455 Views)