09-24-2013 09:43 AM
<-->[^<]*<xx>
09-24-2013 01:39 PM
@DavidU wrote:
I've been trying to get my head around regex but I'm stuck.... The string consists of blocks of data separated by markers, unfortunately not all data blocks were complete, so I have to search through the string to find complete blocks...
As a bonus, if I could have every complete data block returned, that would be fantastic.
Hey David, have a go with Lookarounds and this regex: (?s)(?<=<-->)(?:.(?!<-->))*?(?=<xx>)
Note that I solved this with a pair of positive/negative lookbehinds on your starting match of <-->; you may be able to rewrite this with a pair of positive/negative lookaheads on the ending match of <xx>.
The leading (?s) is just a mode modifier which directs the regex engine to match newlines with the dot.
One thing I like about lookarounds is they are zero-width assertions, meaning you can return content between two anchors while excluding the anchors themselves from the match. You may want to include the whitespace \n as part of your <--> and <xx> anchors to avoid an additional Trim Whitespace on the content of the matches.
09-24-2013 06:34 PM
@JackDunaway wrote:
Hey David, have a go with Lookarounds and this regex: (?s)(?<=<-->)(?:.(?!<-->))*?(?=<xx>)
Wow. That looks like something I'd expect to see in the channel tunnel.
09-27-2013 08:59 AM - edited 09-27-2013 09:01 AM
Thanks to both paulmw and Jack for your replys, I think I understand Jack's solution...
Extra Kudos to Jack for his mind reading abilities. (Unintentionally) I hadn't actually specified the whole of the problem. The Data Blocks themselves contain sub blocks marked with <-1> ... <x1> etc. Jack's solution passes these through successfully.
David
09-29-2013 07:05 PM - edited 09-29-2013 07:18 PM
Hi,
I'm back.
I've been testing the regex from Jack. Under some circumstances LabVIEW crashes at the Match Regular Expression subVI. If the string contains at least one <--> then for a given string length then LabVIEW crashes. Attached is a test vi. The search string contains 1888 characters including the initial "<-->". If you add another character after <--> and run the vi it crashes. Add it before the <--> and the vi runs fine. Is this down to some limit on the lookaround search length?
LabVIEW 8.6.1f1
Any help appreciated
David
09-30-2013 12:14 PM
@DavidU wrote:
Under some circumstances LabVIEW crashes at the Match Regular Expression subVI. If the string contains at least one <--> then for a given string length then LabVIEW crashes. Attached is a test vi. The search string contains 1888 characters including the initial "<-->". If you add another character after <--> and run the vi it crashes. Add it before the <--> and the vi runs fine. Is this down to some limit on the lookaround search length?
David, i'm unable to crash LabVIEW even with a string length of >1MB (with LV 12.0.1f4 32-bit). I notice your signature says LV8.6.1 -- it's possible that LabVIEW or the underlying PCRE library has been enhanced since then to address the issue you're seeing. (If this is the case, you may be relegated to slice-n-dice with more primitive string splitting methods 😕 )
09-30-2013 01:31 PM - edited 09-30-2013 01:32 PM
Need help with this regex/search and replace. I have a string that is [ABC][XYZ]. I need to search for ABC between the first set of brackets, and if there is a match, I need to replace XYZ. So, my output would be [ABC][NEW]. Essentially if there is a match, keep the ABC in brackets but replace whats in the second set of brackets with a user specified string. I have no problem searching for [ABC] and if a match creating [NEW] then concatating the string back up. But I'm curious if there is a way to do it in a single primative call to search and replace.
09-30-2013 04:06 PM
@for(imstuck) wrote:
Need help with this regex/search and replace. I have a string that is [ABC][XYZ]. I need to search for ABC between the first set of brackets, and if there is a match, I need to replace XYZ. So, my output would be [ABC][NEW]. Essentially if there is a match, keep the ABC in brackets but replace whats in the second set of brackets with a user specified string. I have no problem searching for [ABC] and if a match creating [NEW] then concatating the string back up. But I'm curious if there is a way to do it in a single primative call to search and replace.
Looks like nothing a couple lookarounds as zero-width assertions can't handle.
Try (?<=\[ABC\]\[)(.*?)(?=\]) with the Search and Replace String Node, ensuring to turn on Regular Expression matching via it's right-click menu option.
09-30-2013 07:45 PM - edited 09-30-2013 07:45 PM
@JackDunaway wrote:
@for(imstuck) wrote:
Need help with this regex/search and replace. I have a string that is [ABC][XYZ]. I need to search for ABC between the first set of brackets, and if there is a match, I need to replace XYZ. So, my output would be [ABC][NEW]. Essentially if there is a match, keep the ABC in brackets but replace whats in the second set of brackets with a user specified string. I have no problem searching for [ABC] and if a match creating [NEW] then concatating the string back up. But I'm curious if there is a way to do it in a single primative call to search and replace.
Looks like nothing a couple lookarounds as zero-width assertions can't handle.
Try (?<=\[ABC\]\[)(.*?)(?=\]) with the Search and Replace String Node, ensuring to turn on Regular Expression matching via it's right-click menu option.
Nice, Jack. I know the regex option, but as per usual, the real issue is always the regex!
10-02-2013 10:11 PM - edited 10-02-2013 10:22 PM
for(imstuck) wrote: Nice, Jack. I know the regex option...
Oh, totally, didn't intend to speak down to an old pro like yourself. 😄
But I speak for myself, as a "newer" developer when I first found out about this hidden feature of the "Search and Replace" node, it was like Christmas morning convolved with an orthogonal plane of programming capability....
My hope is that a well-documented post gives an early Christmas to someone else, just as I have enjoyed so many early Christmases from others on this helpful forum 🙂