BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular Expressions Board

<-->[^<]*<xx>

Message 111 of 150
(6,002 Views)

@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>)

 

Lookarounds.png

 

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.

Message 112 of 150
(5,971 Views)

@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.

Message 113 of 150
(5,947 Views)

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

0 Kudos
Message 114 of 150
(5,884 Views)

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

0 Kudos
Message 115 of 150
(5,831 Views)

@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 😕 )

0 Kudos
Message 116 of 150
(5,797 Views)

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.

 

 

0 Kudos
Message 117 of 150
(5,789 Views)

@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.

 

SearchAndReplaceWithLookarounds.png

Message 118 of 150
(5,771 Views)

@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.

 

SearchAndReplaceWithLookarounds.png


Nice, Jack. I know the regex option, but as per usual, the real issue is always the regex!

Message 119 of 150
(5,763 Views)

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 🙂

 

Spoiler
(**EDIT** : Ah, and in the spirit of keeping high a SNR on this regex board, here's an actual recent regex with some lookarounds from production code: (?:/s*)?(?<!\\)"(.*?)(?<!\\)"(?:/s*)?:(?:/s*)?(?<!\\)"(.*?)(?<!\\)"  One might say, "It's not fun til your regular expressions start to look like Brain**bleep**." Likewise, a saner mind might say, "It's no fun once your regular expressions start to look like Brain**bleep**")

**EDIT 2** : LOL, Lithium forum parser, you're so silly when it comes to curse words and special character sequences
Message 120 of 150
(5,711 Views)