LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Replace series of 0s with one 0


@zerotolerance wrote:
 But I just saw Darin's example and looks simpler (code wise) than yours. So I'm thinking if regex has any shortfalls (in this case) compared with non-regex version. 

 

Kas



The one shortfall I see about Regex'es is that they are rather cryptic.  If you are a wizard with them, they make sense.  But even if you are a very experienced programmer, they can be very difficult to understand.  I find it difficult to decode and existing Regex.  Trying to create one from scratch on my own is even more difficult.  So I'd prefer to create LabVIEW code that I can understand to step through the functions and flowchart I'd have in my mind.  Even if I was a wizard at Regex, it is very unlikely that the person who might look at my code after me would be able to decode it and understand it.  So a LV version, even if it doesn't appear to be as efficient, would still be a better choice.

Message 11 of 25
(652 Views)

RavensFan wrote:


The one shortfall I see about Regex'es is that they are rather cryptic.  If you are a wizard with them, they make sense.  But even if you are a very experienced programmer, they can be very difficult to understand.  I find it difficult to decode and existing Regex.  Trying to create one from scratch on my own is even more difficult.  So I'd prefer to create LabVIEW code that I can understand to step through the functions and flowchart I'd have in my mind.  Even if I was a wizard at Regex, it is very unlikely that the person who might look at my code after me would be able to decode it and understand it.  So a LV version, even if it doesn't appear to be as efficient, would still be a better choice.


I see what you mean. I'm now trying to decypher Darin's regex (not as straight foward as I thought). However, being complicated/cryptic as they are, they seem to have great advantages. A single regex function replaced the whole structure that  provided.

 

Thanks

 

Kas

0 Kudos
Message 12 of 25
(646 Views)

@altenbach wrote:

You should benchmark it. Are the strings big enough that it actually matters?


For a random [0,1] input string of 1M characters, I get the following results:

 

My code: 0.2 seconds

Regex: 20 seconds (100x slower!)

 

So the regex solution is significantly less efficient. Also, my code could probably be further optimized.

0 Kudos
Message 13 of 25
(636 Views)

RegExes are a simple and powerful declarative language which can be interpreted rather literally so they are not as mysterious as they first appear.

 

(0|1)\1{5,}

 

(0|1) : capture a 0 or 1 () captures, | is alternation

\1 : reference to the first captured object, in this case a 0 or 1

{5,} : five or more

 

This looks for a digit followed by 5 or more of itself, which means greater than 5 0's or 1's in a row

 

$1 simply means replace the pattern with the first capture group, the single character you matched.

 

You can dynamically change the pattern to be three or more, six or more, more than two and less than ten, quite easily.  Try that with a Case Structure.

 

And what I posted in the first message is a snippet, you can drag the image to a BD assuming you have LV9 or later.

 

The benefits of Regexes can be seen in a common interaction on the forum:

 

OP:  Here is a problem

😧 Here is the regex solution

OP: WTF

AB: Here is the non-regex solution

OP:  oops, I have to completely change the problem

AB:  Here is a completely reworked non-regex solution

😧  Simply change a character or two of your regex. ( I am usually slow here since I figured the problem is solved).

Message 14 of 25
(630 Views)

Hi 

I did the default one but the limit case for the maximum number of 1s or 0s is not clear for me. Lets say I have 11 number one 1s. I would like to split it and encode it to 11 (one for every 5 1s). I would greatly appreciate if you could help me on the innercase for the max limit. I tried to reset  the counter but I don't think it is the right way.

Also how can I dynamically change the case structure subdiagram values. I used 5 just as an example. This value might be 100 or 10000 depend on other things.

Many thanks

0 Kudos
Message 15 of 25
(628 Views)

@tintin_99 wrote:

 I tried to reset  the counter but I don't think it is the right way.


Show us what you did

 


@tintin_99 wrote:

Also how can I dynamically change the case structure subdiagram values. I used 5 just as an example. This value might be 100 or 10000 depend on other things.


Instead of wiring the number, you can do a comparison operation with a control and wire the resulting boolean to the case structure.

0 Kudos
Message 16 of 25
(621 Views)

Hi Darkin,

 

can I use RegExes for this coding:

 

111110000011111111111111000   

1011

 

less than 5 = empty

greater than 5  less than 7 = 1 or 0    so for example 14 one should be encoded to 11 not single 1

0 Kudos
Message 17 of 25
(616 Views)

(0|1)\1{0,3}(\1?+)(\1{0,2})

Message 18 of 25
(611 Views)

@tintin_99 wrote:

Hi Darkin,

 

can I use RegExes for this coding:

 

111110000011111111111111000   

1011

 

less than 5 = empty

greater than 5  less than 7 = 1 or 0    so for example 14 one should be encoded to 11 not single 1


Your problem is poorly defined! For example you don't define the case with "equal 5" or with "greater than 7".

 

Do you simply want a character output for each full multiple of five consecutive such characters?

0 Kudos
Message 19 of 25
(607 Views)

Something like this?

 

 

0 Kudos
Message 20 of 25
(602 Views)