Hi Monks
I am looking for a RegEx which search for 'ax', not followed by 'ba' and preplaces this string.
I have two not fully working solutions:
1) One with look ahead => Problem: The chars in the look ahead are not replaced => first term in @reg
2) With two not char sets => Problem: The two charsets are looked speratly, not in combination => second term in @reg.
Many thanks for any help !!
Please find here my testbench:
Program output =>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 ); 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 } }
1..10 --------------- (?^:ax(?!ba)) --------------- ok 1 - (0) axba => axba ok 2 - (1) axbb => -bb ok 3 - (2) axccaxdd => -cc-dd ok 4 - (1) axbaxcc => axb-cc not ok 5 - (1) axcaxcc => -c-cc # Failed test '(1) axcaxcc => -c-cc ' # at Y:\atp01082\data\data\Perl\test\regex_not.pl line 30. # got: 2 # expected: 1 --------------- (?^:ax[^b][^a]) --------------- ok 6 - (0) axba => axba not ok 7 - (1) axbb => axbb # Failed test '(1) axbb => axbb ' # at Y:\atp01082\data\data\Perl\test\regex_not.pl line 30. # got: # expected: 1 ok 8 - (2) axccaxdd => -- ok 9 - (1) axbaxcc => axb- ok 10 - (1) axcaxcc => axc- # Looks like you failed 2 tests of 10.
In reply to RegEx : search for 'ax', not followed by 'ba' by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |