Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Regex question - capturing next char

by perluser4102 (Initiate)
on Oct 10, 2014 at 06:11 UTC ( [id://1103373]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex question - capturing next char
in thread Regex question - capturing next char

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;

Replies are listed 'Best First'.
Re: Regex question - capturing next char
by jonadab (Parson) on Oct 10, 2014 at 10:25 UTC

    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

Re^3: Regex question - capturing next char
by AnomalousMonk (Archbishop) on Oct 10, 2014 at 15:03 UTC

    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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-18 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found