How to make working this regex in perl?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make working this regex in perl?
# 1  
Old 12-02-2014
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 and it works, butwhen I try to use it with the perl command below I get the error shown.


How to fix this? I'm using Cygwin.
$ echo "zghs46RR45ab57cdRR45efghRR7ij@klmRR4...uvw27z@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.
# 2  
Old 12-02-2014
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%
# 3  
Old 12-02-2014
Hello Corona,

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

against the string

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 "%".

# 4  
Old 12-02-2014
Originally Posted by Ophiuchus
Hello Corona,

The Regex does what I want.
Which is... what?
# 5  
Old 12-02-2014
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..

Last edited by Scrutinizer; 12-02-2014 at 05:08 PM..
# 6  
Old 12-02-2014
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 $_'



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.
# 7  
Old 12-02-2014
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

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


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.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Regex not working

I am using a regex to exactly match a string abcdef as ^abcdef$. But it does'nt seem to work :( (11 Replies)
Discussion started by: gaurav99
11 Replies

3. Shell Programming and Scripting

If statement with [[ ]] and regex not working as expected

Using BASH: $ if -- ::00" ]]; then echo "true"; else echo "false"; fi false Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

4. UNIX for Dummies Questions & Answers

Gsub regex not working

I have a number of files that I pass through awk/gsub. I believe to have found a working regex and on 'test bed' sites it matches, however within gsub it does not. Examples: Initial data: /Volumes/Daniel/Public/Drop Box/_Hellsing_Ultimate_OVA_-_10_.mkv gsub & regex: gsub("\]+\]","" ... (4 Replies)
Discussion started by: unknownn
4 Replies

5. Shell Programming and Scripting

need to grep contents of a file within specific time span. regex i am using is not working

Hi , I am trying to extract contents of a file between specified time stamp. but it does not seem to work. i am trying to extract output of /var/adm/messages between 15:00:00 to 15:23:59 . i have tried two regex the first one seems to kind of work. it displays some output. the second one is... (13 Replies)
Discussion started by: chidori
13 Replies

6. Shell Programming and Scripting

matching a regex using egrep not working

Hi, I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters? if echo 'en-GB' | egrep '({1,8})(-{1,8})*' >/dev/null; then echo Valid value fi Thanks in advance (6 Replies)
Discussion started by: skrtxao
6 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. Shell Programming and Scripting

bash with: if, elif & regex not working

Why is only hello3 being printed? There must be some kind of syntax problem because the file list definitely includes all the file extensions line by line. #!/bin/bash find '/home/myuser/folder/' -name '*.c' -type f | while read F do if ] # if the file name ends in .txt.c then ... (6 Replies)
Discussion started by: cyler
6 Replies

9. Shell Programming and Scripting

BASH regex (convert from working perl version)

Hi there, I need to test that a variable ($VAR) matches a regex mask in BASH. I have the exact thing working in perl (below), but could somebody advise me how i would do the same in BASH ? do i need to use something like egrep ? #!/bin/perl -w my $VAR = "some value"; if ( $VAR =~... (4 Replies)
Discussion started by: rethink
4 Replies

10. Shell Programming and Scripting

gnu sed regex grouping not working?

Hello, from the gnu sed manual, I should be able to do this: `\(REGEXP\)' Groups the inner REGEXP as a whole, this is used to: * Apply postfix operators, like `\(abcd\)*': this will search for zero or more whole sequences of `abcd', while `abcd*' ... (3 Replies)
Discussion started by: Allasso
3 Replies
Login or Register to Ask a Question

Featured Tech Videos