[tpm] Regex question

Liam R E Quin liam at holoweb.net
Tue Nov 6 11:24:13 PST 2007

On Tue, 2007-11-06 at 13:39 -0500, Indy Singh wrote:
> Hello all,
> I have some multiline data as shown below.  I want to remove all 
> occurances of 'foo.*?' that are at the beginning of a line.  How can I 
> do that witha regex.  I can do it with a while loop, but is a there a 
> more elegent way with a single regex?

$x =~ s/^foo.*?//gm;

The m modifier means to treat the string as having multiple lines,
so that ^ matches the start of a line.

The g (global) means to do it everywhere in the input.

You can also write this as,
$x =~ s{^foo.*?}{}gm;

and, for more complex expressions,

$x =~ s{
    ^            # start of line
    foo          # first three letters of foot
    .*?          # this will match the rest of the line

Note that ^foo.* will match the same as ^foo.*? here, because
the . can't match a newline without the /s flag.


Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org www.advogato.org

More information about the toronto-pm mailing list