Home > Archive > PERL Miscellaneous > May 2005 > Regex - disincluding strings in a match
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Regex - disincluding strings in a match
|
|
| Steve Chiang 2005-05-27, 8:57 pm |
| Hello,
How would one disinclue a string in a match? For example, I want to match
the next animal that comes after "dog", but don't want to match "dog":
cat cow dog monkey zebra
.... so I would want to match only "monkey". However, the order of the items
in the list may change anytime:
zebra monkey dog cow cat
.... so I would want to match only "cow" in this case since it follows "dog"
directly. Further, I am limited in that I cannot use back-references for
this.
Thanks,
Steve
| |
| Eric Schwartz 2005-05-27, 8:57 pm |
| "Steve Chiang" <stevo@berkeley.edu> writes:
> How would one disinclue a string in a match? For example, I want to match
> the next animal that comes after "dog", but don't want to match "dog":
>
> cat cow dog monkey zebra
my $string = "cat cow dog monkey zebra";
my $animal = "dog";
my (undef, $match) = $string =~ /(\Q$animal\E) (?!\1)(\w+)/;
print "Match: [$match]\n";
Look up "negative lookahead" in perlre for more details.
> ... so I would want to match only "cow" in this case since it follows "dog"
> directly. Further, I am limited in that I cannot use back-references for
> this.
Why not? Perl supports backreferences. If you're trying to do this
in a non-Perl language that supports regexes, then you'd best ask
there; regexes very a LOT between languages, and the only way to know
for sure is to check the docs for whatever language you're using.
Anyway, I'm sure you can see how to avoid backreferences in that code;
I'll leave eliminating them as an exercise for the reader.
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
| |
| ioneabu@yahoo.com 2005-05-27, 8:57 pm |
|
Eric Schwartz wrote:
> "Steve Chiang" <stevo@berkeley.edu> writes:
>
> my $string = "cat cow dog monkey zebra";
> my $animal = "dog";
> my (undef, $match) = $string =~ /(\Q$animal\E) (?!\1)(\w+)/;
> print "Match: [$match]\n";
>
Why not simply:
#!/usr/bin/perl
use strict;
use warnings;
my $string = "cat cow dog monkey zebra";
$string =~ /dog\s*(\w+)/;
print $1;
I know I am not really matching "the word after dog" exactly, but the
outcome is the same I think.
wana
| |
| Eric Schwartz 2005-05-27, 8:57 pm |
| ioneabu@yahoo.com writes:
> Eric Schwartz wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^[color=darkred]
> Why not simply:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $string = "cat cow dog monkey zebra";
>
> $string =~ /dog\s*(\w+)/;
That matches "dog dog";
> print $1;
My solution was just a touch more flexible than required (i.e., I made
the animal to look for variable), but definitely won't match "dog
dog" (assuming $animal eq "dog").
> I know I am not really matching "the word after dog" exactly, but the
> outcome is the same I think.
Actually, that's exactly what you are matching. It's just not what
the OP asked for.
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
| |
| Fabian Pilkowski 2005-05-27, 8:57 pm |
| * Steve Chiang schrieb:
>
> How would one disinclue a string in a match? For example, I want to match
> the next animal that comes after "dog", but don't want to match "dog":
>
> cat cow dog monkey zebra
>
> ... so I would want to match only "monkey".
> Further, I am limited in that I cannot use back-references for
> this.
You could use a technique called "zero-width positive look-behind" and
is described in `perldoc perlre`. Unless you have perl installed on your
system read this document at <http://perldoc.perl.org/perlre.html>.
my $string = "cat cow dog monkey zebra";
print $string =~ m/(?<=dog\s)\S*/g;
__END__
monkey
regards,
fabian
| |
| Christopher Nehren 2005-05-27, 8:57 pm |
| On 2005-05-27, Steve Chiang scribbled these
curious markings:
> Further, I am limited in that I cannot use back-references for this.
Then perhaps you should ask your teacher for help if you can't figure it
out on your own.
Best Regards,
Christopher Nehren
--
I abhor a system designed for the "user", if that word is a coded
pejorative meaning "stupid and unsophisticated". -- Ken Thompson
If you ask the wrong people questions, you get "Joel on Software".
Unix is user friendly. However, it isn't idiot friendly.
| |
| xhoster@gmail.com 2005-05-27, 8:57 pm |
| "Steve Chiang" <stevo@berkeley.edu> wrote:
> Hello,
>
> How would one disinclue a string in a match? For example, I want to
> match the next animal that comes after "dog", but don't want to match
> "dog":
You either match the string, or you do not match the string. You cannot
match part of a string. You can *capture* part of a string. Is that
what you meant to ask?
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Tad McClellan 2005-05-28, 3:58 am |
| ioneabu@yahoo.com <ioneabu@yahoo.com> wrote:
> Eric Schwartz wrote:
[snip Eric's code]
[color=darkred]
> Why not simply:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $string = "cat cow dog monkey zebra";
>
> $string =~ /dog\s*(\w+)/;
>
> print $1;
You should never use the dollar-digit variables unless you
have first ensured that the match *succeeded*.
print $1 if $string =~ /dog\s*(\w+)/;
> I know I am not really matching "the word after dog" exactly, but the
> outcome is the same I think.
What does it match when you try it with
$string = "cat cow dogma monkey zebra";
??
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Eric Amick 2005-05-28, 3:58 am |
| On Fri, 27 May 2005 23:11:17 +0200, Fabian Pilkowski
<pilkowsk@informatik.uni-marburg.de> wrote:
>* Steve Chiang schrieb:
>
>
>You could use a technique called "zero-width positive look-behind" and
>is described in `perldoc perlre`. Unless you have perl installed on your
>system read this document at <http://perldoc.perl.org/perlre.html>.
>
> my $string = "cat cow dog monkey zebra";
> print $string =~ m/(?<=dog\s)\S*/g;
> __END__
> monkey
Almost--that will match the second "dog" in "dog dog". What you need is
print $string =~ m/(?<=dog\s)(?!dog)\S+/g;
--
Eric Amick
Columbia, MD
|
|
|
|
|