How to make working this regex in perl?

Hello to all,

The Regex below is supposed to match all strings except RR45. I've tested in regex101.com and it works, butwhen I try to use it with the perl command below I get the error shown.

Regex=(?<=^|RR45)(?!RR45).+?(?=RR45|$)

How to fix this? I'm using Cygwin.

$ echo "zghs46RR45ab57cdRR45efghRR7ij@klmRR45noCODpqrRR45stuvw27z@xyRRR45" | perl -nle 's/(?<=^|RR45)(?!RR45).+?(?=RR45|$)/%/g'
Variable length lookbehind not implemented in regex m/(?<=^|RR45)(?!RR45).+?(?=RR45|$)/ at -e line 1.

Thanks for any help.

What exactly is the intention here? It's hard to tell what you do want, from a regex which doesn't do what you want.

If you want to replace everything but RR45 with %, match .*RR45.* and replace it with %RR45%

Hello Corona,

The Regex does what I want. you can test in regex101.com the regex

 
(?<=^|RR45)(?!RR45).+?(?=RR45|$)

against the string
[EMAIL="zghs46RR45ab57cdRR45efghRR7ij@klmRR45noCODpqrRR45stuvw27z@xyRRR45"]

zghs46RR45ab57cdRR45efghRR7ij@klmRR45noCODpqrRR45stuvw27z@xyRRR45

[/EMAIL]
And you'll see that matches all strings except RR45.

I only want to test this regex using perl command instead to test it in online regex testers.

Then if it works in perl command, the strings different than RR45 should be replaced by "%".

Regards

Which is... what?

The error message is what it says. The lookbehind can not be of variable length. That means in this case that the use of alternation ( | ) in (?<=^|RR45) is not allowed because the two sides ( ^ and RR45 ) are not the same length..

I think this will do what you want:

$ echo "zghs46RR45ab57cdRR45efghRR7ij@klmRR4...uvw27z@xyRRR45" | perl -nle 's/RR45/\xff/g;s/[^\xff]+/%/g;s/\xff/RR45/g ; print $_'

%RR45%RR45%RR45

$

Replace what you want with something easily matchable, delete what you don't want, and restore it. This avoids needing strange convolution, backreferences, or non-greedy matching.

Another method would be splitting on RR45, changing all non-blank tokens into "%", then imploding again.

Thank you Scrutinizer and Corona for your help.

Corona, thanks for you solution, but actually I want to apply certain process for each matched string and I tried to replace with "%" only in order to test if the regex captures the strings different than RR45 that are those what I need.

Then, for example if the string is the one below
[EMAIL="zghs46RR45ab57cdRR45efghRR4ij@klmRR45vw27z@xyRRR45"]

zghs46RR45ab57cdRR45efghRR4ij@klmRR45vw27z@xyRRR45

[/EMAIL]

Then, I'd like to match the following strings in order to process each one later.

 
zghs46
ab57cd
efghRR4ij@klm
vw27z@xy

I hope make sense why I'm trying to use that complex regex. Using split I think it could work, only that generates empty strings when RR45 is at the beginning of the string.

Regards

You want to break a string into parts upon a regular expression? That's exactly what "split" is for.

You can get rid of the beginning "" with shift(@arr) if you need to, but it's real -- ditch it and you're effectively changing the number of RR45's found.

Not regex, but one way would be:

awk -F'RR45' '{for(i=1; i<=NF; i++) if($i~/./) print $i}'
zghs46
ab57cd
efghRR4ij@klm
vw27z@xyR

--edit--
With perl this could work:

perl -pe 's/(^|RR45)(.+?)(?=RR45|$)/$2\n/g; s/(RR45)?\n$//'

-or-
Or perhaps simply:

perl -pe 's/RR45/\n/g; s/^\n|\n$//'

Hello Corona,

Many thanks for your suggestions and help.

Hello Scrutinizer,

Both solutions work so fine and I've seen your regex that works in other but efective way.

Many thanks to both.

Regards