LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Another RegEX question

Solved!
Go to solution

Hi, I have a question regarding RegEx: If you take a look at the attached code you will find two examples. This is an old piece of code, but now I have to fix a bug that was discovered after a couple years of useage. Task: If the input is for example C1-C4, the algorithm needs to output an array of C1,C2,C3,C4. This has to work on any number and letter. Examples: R1-R5, K569-K799, S2-S3. This works great for [Letter][Number], but my regex will not work for the case I now have to solve. If the input is C1A1-C1A4 the output should be C1A1,C1A2,C1A3,C1A4, but I don't know what the new RegEx should look like so it still works on [Letter][Number][Letter][Number].

Regards,
Even
_________________________________
Certified LabVIEW Associate Developer

Automated Test Developer
Topro AS
Norway
0 Kudos
Message 1 of 11
(3,302 Views)

I have some bugs with the forum and Opera so when I created the post it removed all my line feeds. Here is my post again, hopefully easier to read:

 

Hi, I have a question regarding RegEx:

 

If you take a look at the attached code you will find two examples. This is an old piece of code, but now I have to fix a bug that was discovered after a couple years of useage.

 

Task: If the input is for example C1-C4, the algorithm needs to output an array of C1,C2,C3,C4. This has to work on any number and letter. Examples: R1-R5, K569-K799, S2-S3. This works great for [Letter][Number], but my regex will not work for the case I now have to solve.

 

If the input is C1A1-C1A4 the output should be C1A1,C1A2,C1A3,C1A4, but I don't know what the new RegEx should look like so it still works on [Letter][Number][Letter][Number]. 

Regards,
Even
_________________________________
Certified LabVIEW Associate Developer

Automated Test Developer
Topro AS
Norway
0 Kudos
Message 2 of 11
(3,293 Views)

Change "[a-zA-Z]+" to "[a-zA-Z]+[0-9]+[a-zA-Z]+".

 

Of course, this is based on the explicit pattern you specified, whereby the second "number" is the one that's being incremented.

Message 3 of 11
(3,273 Views)

I'm not a regex expert, so you can probably find a better solution.

It seems to work though.

 

Regards Florian

Message 4 of 11
(3,267 Views)

I didn't have a look at the proposed solutions (I'm still using LV2009) but here is how I would do it using capture groups and backreference.

 

Ben64

 

regex solution.png

Message 5 of 11
(3,259 Views)

I'm sorry - learning to create a snippet was not easy with the german version of LabVIEW.

Ok and now I can't just copy the png into the post. I get "Message cannot exceed 10,000 characters." and a quick forum search didn't help.

How do I post a snippet?

 

Ben64: Your version can't cope with the old strings that contain only one letter.

 

Regards Florian

Message 6 of 11
(3,248 Views)

@Florian.Ludwig wrote:

I'm sorry - learning to create a snippet was not easy with the german version of LabVIEW.

Ok and now I can't just copy the png into the post. I get "Message cannot exceed 10,000 characters." and a quick forum search didn't help.

How do I post a snippet?

 

Ben64: Your version can't cope with the old strings that contain only one letter.

 

Regards Florian


Sorry, didn't know it was still required. Try this regex : ([a-zA-Z]+[0-9]+[a-zA-Z]+|[a-zA-Z]+)(\d+)-\1(\d+)

 

Ben64

Message 7 of 11
(3,239 Views)

@Florian.Ludwig wrote:

I'm sorry - learning to create a snippet was not easy with the german version of LabVIEW.

Ok and now I can't just copy the png into the post. I get "Message cannot exceed 10,000 characters." and a quick forum search didn't help.

How do I post a snippet?


You need to attach the image to the post using the attachments, or upload it to your image gallery. It's an image, so it's like including any other image, as indicated in the forum help: http://forums.ni.com/t5/help/faqpage/faq-category-id/images#images

 

By the way, the Code Capture Tool is superior to the build-in snippet.

Message 8 of 11
(3,236 Views)
Solution
Accepted by topic author EvenDeejay

Can you not just use [0-9]+$ to return only the number from the end of the string?

 

Match Regular Expression.vi will return everything before the final number in "before match".

 

Sorry I can't look at your code since I do not have LabVIEW 2011 on my system.

 

     Rob

Message 9 of 11
(3,228 Views)

@Robert Cole wrote:

Can you not just use [0-9]+$ to return only the number from the end of the string?


I love it when we miss the obvious solutions. Smiley Wink

Message 10 of 11
(3,221 Views)