Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Regex question - capturing next char

by perluser4102 (Initiate)
on Oct 10, 2014 at 01:46 UTC ( [id://1103362]=perlquestion: print w/replies, xml ) Need Help??

perluser4102 has asked for the wisdom of the Perl Monks concerning the following question:

I need to replace 'aa' with 'a' in a string if 'aa' occurs at the end of the word. But the characters in the string could be separated by meta data of the form: <meta_data>, where meta_data could be anything.

Please look at this snippet of code. My goal is to check if there is a character following 'aa'. When I execute this code, the "if" loop is entered. I would think the 'r' would fail a match at(^a-zA-Z*) and the if block won't be entered.

my $in_str = 'aa<c><d>r'; if ($in_str =~ /^aa((<[^>]*>)*)([^a-zA-Z]*)/) { print "Found 1: $1\n"; print "Found 2: $2\n"; print "Found 3: $3\n"; }

Replies are listed 'Best First'.
Re: Regex question - capturing next char
by LanX (Saint) on Oct 10, 2014 at 02:12 UTC
    multiple problems:

    • you say "word" but you mean string, right?
    • you don't anchor to the end of the string with $ but to the start with ^
    • you use an all quantifier * for the negated class, which also means 0-times, so r doesn't need to be rejected and the if-clause is entered.

    Actually I don't know any more what your real goal is, so no recommendation other than

    DB<114> $in_str = 'aa<c><d>r'; => "aa<c><d>r" DB<115> if ($in_str =~ /aa((<[^>]*>)*)$/) {print "$1"} => "" DB<116> $in_str = 'aa<c><d>'; => "aa<c><d>" DB<117> if ($in_str =~ /aa((<[^>]*>)*)$/) {print "$1"} => 1 <c><d>
    maybe try to show us better input and desired output.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Thanks for the replies. Let me explain my goal using a few examples:

      Case 1:

      $in_str = ‘somewordaa<metadata><moremetadata>r another wo<metadata>rd’; => aa should not be replaced by a, since aa is immediately followed by another alphabet 'r' once the metadata is ignored.

      Case 2:

      $in_str = ‘aword<metadata>aa<metadata> another wo<metadata>rd’; => aa should be replaced by a because it is at the end of a word. It is followed by space.

      Case 3:

      $in_str = 'wor<metadata>dsandmor<metadata>ewordsaa<metadata>;' => aa should be replaced by a because aa is at the end of the word (also end of string) once metadata is ignored.

      I now understand that the quantifier * on a negation of character class won't work because of the "0 times" match. I need to check if aa is at the end of a word. I plan to do this by checking if the following character (again, after ignoring metadata) is either a non-alphabet character or end of string. How would I go about doing this?

      $in_str =~ s/aa((<[^>]*>)*)([^a-zA-Z])/a$1$3/g;

        I think you can do this with a single substitution regex (s/pattern/replacement/;), but it's going to need a lookahead assertion at the end. Also, this is only possible if you can write a regex segment that will consistently match the metadata. (Making it match metadata if there is any and not block matching if there isn't any is then trivial: precede the metadata-match portion in (?: and follow it up with )*.

        Perhaps something along these lines...

        s/(a(?:[<][^>]+[>])*)a((?:[<][^>]+[>])*(?:\s+|[;,.])+)/$1$2/;

        That appears to work with the actual sample data you provided, but I don't know if it will work with your real data.

        I would approach the problem this way:

        c:\@Work\Perl>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; my $metadata = qr{ < [^>]* > }xms; ;; for my $ar_vector ( [ 'somewordaa<metadata><moremetadata>r another wo<metadata>rd', 'somewordaa<metadata><moremetadata>r another wo<metadata>rd', ], [ 'aword<metadata>aa<metadata> another wo<metadata>rd', 'aword<metadata>a<metadata> another wo<metadata>rd', ], [ 'wor<metadata>dsandmor<metadata>ewordsaa<metadata>', 'wor<metadata>dsandmor<metadata>ewordsa<metadata>', ], ) { my ($s, $expected) = @$ar_vector; $s =~ s{ aa (?! $metadata* r) }{a}xmsg; is $s, $expected, qq{'$expected'}; } " ok 1 - 'somewordaa<metadata><moremetadata>r another wo<metadata>rd' ok 2 - 'aword<metadata>a<metadata> another wo<metadata>rd' ok 3 - 'wor<metadata>dsandmor<metadata>ewordsa<metadata>' ok 4 - no warnings 1..4
        Note that making the character class match 'atomic' or possessive may speed things up a bit for large data (for some definition of 'large') by suppressing backtracking on subsequent match failure:
            my $metadata = qr{ < (?> [^>]*) > }xms;
        and for Perl version 5.10 and above:
            my $metadata = qr{ < [^>]*+ > }xms;
        (both variations tested). Your data will determine if possessiveness actually makes a difference. The regex compiler may be smart enough to make this optimization on its own (I haven't checked), but giving the compiler a hint never hurts.

        And of course, you will want to add many more test cases!

Re: Regex question - capturing next char
by Athanasius (Archbishop) on Oct 10, 2014 at 02:09 UTC

    Hello perluser4102, and welcome to the Monastery!

    I would think the 'r' would fail a match at([^a-zA-Z]*) and the if block won't be entered.

    The * quantifier specifies zero or more matches, so in this case it successfully matches zero times and the if condition is true. But if you terminate the regex with an end-of-line metacharacter $, the match fails:

    if ($in_str =~ /^aa((<[^>]*>)*)([^a-zA-Z]*)$/) # ^

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Regex question - capturing next char
by Anonymous Monk on Oct 10, 2014 at 01:47 UTC
    why not split the string into words, then do your checks?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1103362]
Approved by farang
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found