in reply to Regex question - capturing next char

multiple problems:

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 ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: Regex question - capturing next char
by perluser4102 (Initiate) on Oct 10, 2014 at 06:11 UTC

    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.

        And for the degenerative case: [<][^>]+[>] does not allow for an empty <> field. Perhaps [<][^>]*[>] is better?

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

      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!