05-31-2012 06:45 AM - edited 05-31-2012 06:46 AM
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].
Solved! Go to Solution.
05-31-2012 06:59 AM - edited 05-31-2012 07:00 AM
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].
05-31-2012 08:11 AM - edited 05-31-2012 08:12 AM
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.
05-31-2012 08:27 AM
I'm not a regex expert, so you can probably find a better solution.
It seems to work though.
Regards Florian
05-31-2012 09:19 AM
05-31-2012 09:43 AM
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
05-31-2012 10:08 AM - edited 05-31-2012 10:09 AM
@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
05-31-2012 10:23 AM
@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.
05-31-2012 11:22 AM
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
05-31-2012 12:01 PM - edited 05-31-2012 12:02 PM
@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.