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:

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 } }
Program output =>
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.