[Omaha.pm] A less greedy regular expression...

Daniel Linder dan at linder.org
Wed Sep 22 00:13:25 CDT 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ok, another regexp question.

I have a variable with content that looks like this:

$a = "AAAbbbCCCAAAdddCCCAAAdddCCCAAAdddCCC";

Basically the "AAA" and "CCC" strings are begin and end markers for the
text I am interested in (specifically the "bbb" or "ddd" strings).

When I use this command to strip off the "markers"
$a =~ s/AAA(.*)CCC/$1/;

The $a variable ends up containing "bbbCCCAAAdddCCCAAAdddCCCAAAddd" (i.e.
the first "AAA" and the last "CCC" were removed).  What I had hoped for
was to have the first "bbb" returned.

I think the cause of this is that the =~ command is 'greedy' and will
match the longest string it can find.  Since the number and pattern of the
remaining markers are random, is there a flag I can pass via the regexp to
have it match on the first/smallest match?

A work around I am looking at involves the "split" command like this:
($foo, $a, $bar) = split ("AAA|CCC", $a);

Other ideas?

Dan

- - - - -
"I do not fear computer,
I fear the lack of them."
 -- Isaac Asimov

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBUQn1NiBNyqUzGb8RAvEUAJ9HwXHn1tEBitaWeo7jRXXAslwjLgCfTePa
b5mUm2LIEEDW+3ElqmLF1nc=
=lBRP
-----END PGP SIGNATURE-----


More information about the Omaha-pm mailing list