Question

I have a regular string as bellow:

xxSTART Here we have the first text in 1234 asdf xxENDxxSTART Here we have the second text 999 fffd xxENDxxSTART Here we have the third text 1234 9985Df xxENDxxSTART Here we have the fourth text 1234 asdf Dert xxEND

I'm using the follow REGEX: ^(?:(.*?)\K(xxSTART)){3}(.*?xxEND) to get ONLY third match xxSTART Here we have the third text 1234 9985Df xxEND. This work well in http://www.regexr.com/v1/, but i read a article saying \K option is not a option in C# (Support of \K in regex), and the article Translate Perl regular expressions to .NET say to use look-behind ((?<=…)) instead. But i cant use look-behind ((?<=…)) in my RegEX, anybody can help me? please!!

Anyone have a ideia how to use ((?<=…)) in my RegEX ^(?:(.*?)\K(xxSTART)){3}(.*?xxEND) to replace \K option?

Thank Regards,

Was it helpful?

Solution

You don't really need a lookbehind here, you can match the xxSTART and still get the 3rd part that you want to get:

^(?:xxSTART.*?){3}\s*(.*?)xxEND

ideone demo

But if you really want to use a lookbehind (in case you don't want any capture groups, well, in this case, you can consider using a lookahead for the xxEND), you would use something like this:

(?<=^(?:xxSTART.*?){3}\s*).*?(?=xxEND)

ideone demo

OTHER TIPS

Simply use this:

^(?:xxSTART.*?xxEND){2}(xxSTART.*?xxEND)

Skip the first two blocks first, and then capture the third one. No lookbehind assertion is required here.

An easy method is to not impose this limitation inside the regex, but instead to do the counting outside:

use strict;
use warnings;

my $data = do {local $/, <DATA>};

my $count = 0;
while ($data =~ /(?<=xxxSTART)(.*?)(?=xxEND)/g) {
    if (++$count == 3) {
        print $1;
        last;
    }
}

__DATA__
xxSTART Here we have the first text in 1234 asdf xxENDxxSTART Here we have the second text 999 fffd xxENDxxSTART Here we have the third text 1234 9985Df xxENDxxSTART Here we have the fourth text 1234 asdf Dert xxEND

outputs:

 Here we have the third text 1234 9985Df
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top