LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

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

Solved!
Go to solution
Highlighted

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
(1,063 Views)
Solution
Accepted by topic author SergeS
03-16-2018 11:11 PM

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
CLAD expired, using 2009SP1 + LV2011SP1 + LV2017 (+LV2018 sometimes) on Win7+cRIO
Kudos are welcome Smiley Wink

0 Kudos
Message 2 of 3
(1,037 Views)
Solution
Accepted by topic author SergeS
03-16-2018 11:11 PM

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
(1,022 Views)