LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

With regex, how can I combine backreference and repetition

Let's say I have a four byte long input string, consisting of four hex values:  0x03 0x03 0x03 0x03

 

If I use repetition in a regex string pattern: .{4}  it matches true.

Also, if I use backreference in a new regex pattern: (.)\1\1\1  it matches true.

 

But I want to combine the two.  Say that the first byte is a length byte of the following bytes.  Then I'd believe it should be possible to do this: (.).{\1}

The thought is that the first byte is stored as "variable" \1, and reused as the repetition counter.  So in this case, all four byte strings starting with 0x03 should match true.

But this doesn't work! Smiley Happy

 

What am I doing wrong here?  Is this just not possible?

0 Kudos
Message 1 of 5
(3,944 Views)

No RegEx experts here? Smiley Happy

0 Kudos
Message 2 of 5
(3,917 Views)

Here is a web site just for you.

 

http://www.regular-expressions.info/

Tim
GHSP
0 Kudos
Message 3 of 5
(3,899 Views)

 


@ojohnsen wrote:

No RegEx experts here? Smiley Happy


 

I am not an expert, but I play one on this forum.  To show something is possible is quick, since you can demonstrate it.  To show something is impossible is a bit harder.  For now I say, not possible.  And you do realize that .{4} will match any four character string, not anything repeated four times, right?

 

That said, it sounds like you know the format of your string, so perhaps a regex would not be your first choice.  You could split a string, read the first byte, then construct a regex like ^(.)\1{length-1} and see if the remainder of the string begins with a value repeated length times.

0 Kudos
Message 4 of 5
(3,894 Views)

I don't believe you can put a not-integer value after a slash to bring up a backreference.  Further, I don't think you can put a string like \1 into an integer spot inside the curly brakets {3}, the engine has no way to decode that reference (which is of course a string) into an integer and then substitute it to get the ref count.  Since you are in LabVIEW and not in perl you can use the first string, decode it into length, and then build a string to input into the regex function block replacement text field to check all the others appropriate length.

0 Kudos
Message 5 of 5
(3,876 Views)