Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Lookahead/Lookbehind Regular Expression...

by hdb (Monsignor)
on Dec 19, 2013 at 17:15 UTC ( #1067839=note: print w/replies, xml ) Need Help??

in reply to Lookahead/Lookbehind Regular Expression...

I would prefer a two step approach avoiding look(ahead|behind|around) assertions: first find patterns like a repeat of letter followed by a dot, then remove the dots and replace the pattern. Like this:

use strict; use warnings; my $str = "a history of u.s. coast guard aviation. M.C. Esher"; $str =~ s/\W\K((\w\.)+)/ $1 =~ s{\.}{}gr /ge; print "$str\n";

The \W\K at the beginning is required to avoid matching "am.c.". It will also match a single occurence of letter followed by dot but that can be easily fixed by requiring two or more repetitions.

Update: removed the unnecessary non-capturing ?: from the regex.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067839]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-02-18 22:10 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.