pattern question
Author Message
pattern question

In the following string:
\$AA = "a xxx c xxx c xxx c";
how can one be "ungreedy" and match only up to the FIRST c ?  The naive
\$AA =~ s/a.*c/zz/;
gives   zz . This is surely simple (and standard) - but my experiments failed.

Thanks,
Jerry

Thu, 17 Jul 1997 00:30:23 GMT
pattern question

Quote:
>In the following string:
>    \$AA = "a xxx c xxx c xxx c";
>how can one be "ungreedy" and match only up to the FIRST c ?  The naive
>    \$AA =~ s/a.*c/zz/;
>gives   zz . This is surely simple (and standard) - but my experiments failed.

If you are using perl5 then you can do

\$AA =~ s/a.*?c/zz/;
print \$AA,"\n";

zz xxx c xxx c

--
.-----------------------------------------------------------.

|  00  | MOS Design                   IMS: BODG                    |  00  |
C   ^  | Texas Instruments Ltd      Phone: +44 (0)1234 22 3419     |  ^   O
\ ~/  | ENGLAND                      Fax: +44 (0)1234 22 3331     |  \~ /
`-----------------------------------------------------------'

Thu, 17 Jul 1997 00:53:56 GMT
pattern question

Quote:

>In the following string:
>    \$AA = "a xxx c xxx c xxx c";
>how can one be "ungreedy" and match only up to the FIRST c ?  The naive
>    \$AA =~ s/a.*c/zz/;
>gives   zz . This is surely simple (and standard) - but my experiments failed.

\$AA =~ s/a.*?c/zz/; # should work in perl5
\$AA =~ s/a[^c]*c/zz/; # should work in any version, but doesn't
# generalize if the "upto" thingy isn't a
# character or character class.  E.g.,
# /^.*?cat/ matches "crazy cat" but /^[^c]*cat/
# doesn't.
\$AA =~ s/a[^c\n]*c/zz/; # more precise equivalent to .*?c

--
<J Q B>

Thu, 17 Jul 1997 05:56:51 GMT
pattern question
: In the following string:
:       \$AA = "a xxx c xxx c xxx c";
: how can one be "ungreedy" and match only up to the FIRST c ?  The naive
:       \$AA =~ s/a.*c/zz/;
: gives   zz . This is surely simple (and standard) - but my experiments failed.

\$AA =~ s/a[^c]*c/zz/;

Fri, 18 Jul 1997 05:38:08 GMT
pattern question

:In the following string:
:       \$AA = "a xxx c xxx c xxx c";
:how can one be "ungreedy" and match only up to the FIRST c ?  The naive
:       \$AA =~ s/a.*c/zz/;
:gives   zz . This is surely simple (and standard) - but my experiments failed.

Try:
\$AA =~ s/a[^c]*c/zz/;

In Perl5, you can also say:

\$AA =~ s/a.*?c/zz/;

--
Toru "devil-may-care" Shiono          Sony Corporation, JAPAN

Fri, 18 Jul 1997 20:30:16 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages