in reply to RegEx : search for 'ax', not followed by 'ba'

I am looking for a RegEx which search for 'ax', not followed by 'ba' and preplaces this string.

If I may, I'll reword this to what you actually want: "I am looking for a RegEx which searches for 'ax' followed by two chars which are not "ba". In that case, here's a solution:

use strict; use warnings; use Test::More; $\="\n"; # search for 'ax', not followed by 'ba' my @teststr = ('0axba', # simple not ok '1axbb', # one char ok, one not '2axccaxdd', # simple 2 times '1axbaxcc', # one time (sequence combined) '1axcaxcc' # one time (sequence combined) ); my @reg = ( qr/ax(?!ba)/, # look ahead qr/ax[^b][^a]/, # 2 not char sets qr/ax(?!ba)../, # look ahead plus 2 chars ); plan tests => @teststr*@reg; for my $re (@reg) { print "---------------\n$re\n---------------"; for my $str (@teststr) { my $str1=$str; my $str2=$str; # get teststring $str1=~s/^\d//; # remove result my $got = $str1=~s/$re/-/g; # replace (do test) $str2=~s/^(\d)//; # remove result and remember cmp_ok($got, '==', $1, "($1) $str2 => $str1 "); #check } }

So you were really very close.

Replies are listed 'Best First'.
Re^2: RegEx : search for 'ax', not followed by 'ba'
by Anonymous Monk on Mar 16, 2018 at 09:22 UTC
    Create ! Simple solution, exactly what I was looking for ! Many Thanks !!