Home > Archive > PERL Miscellaneous > January 2006 > Regular expression question : how to not match a word
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 |
Regular expression question : how to not match a word
|
|
| linq936@hotmail.com 2006-01-26, 7:00 pm |
| Hi,
I want to use regular expression to match a pattern which does not
have a whole word, let us say the word is "trash", then I tried this
pattern, [^trash], but it does not work. It sort of makes sense that
this does not work in that it does not treat "trash" as a whole word.
Any idea how to make it?
Thanks.
| |
| J. Gleixner 2006-01-26, 7:00 pm |
| linq936@hotmail.com wrote:
> Hi,
> I want to use regular expression to match a pattern which does not
> have a whole word, let us say the word is "trash", then I tried this
> pattern, [^trash], but it does not work. It sort of makes sense that
> this does not work in that it does not treat "trash" as a whole word.
Give the documentation a try. This is right out of perlretut:
"... The sense of the match can be reversed by using !~ operator:
if ("Hello World" !~ /World/) {
print "It doesn't match\n";
}
else {
print "It matches\n";
}
"
| |
| Paul Lalli 2006-01-26, 7:00 pm |
| linq936@hotmail.com wrote:
> I want to use regular expression to match a pattern which does not
> have a whole word, let us say the word is "trash", then I tried this
> pattern, [^trash], but it does not work.
Can you explain what made you think that might work?
That chunk will match any ONE character that is NOT a t, an r, an s, or
an h.
> Any idea how to make it?
The same way that's been recommended in this group aproximately 20
times in the past year. Please do a minimal amount of searching before
posting. Search for terms like "negative match", for example.
Paul Lalli
| |
| Xicheng 2006-01-26, 7:00 pm |
| linq936@hotmail.com wrote:
> Hi,
> I want to use regular expression to match a pattern which does not
> have a whole word, let us say the word is "trash", then I tried this
> pattern, [^trash], but it does not work. It sort of makes sense that
> this does not work in that it does not treat "trash" as a whole word.
that "[]" stuff matches a single char from the character class, not a
whole word. you should take a look at negative look ahead (?!trash) or
negative look behind(?<!trash) assertions.
Xicheng
| |
| batista@bit.uni-bonn.de 2006-01-27, 3:58 am |
| > I want to use regular expression to match a pattern which does not
> have a whole word, ...
If I understand you well you are looking for something like this:
@arr = qw(Use the force, Luke);
$pattern = "uke";
foreach $word (@arr){
if( $word =~ m/$pattern/ && $word ne $pattern){
print "Find pattern match, but it's not the whole word!\n";
}
}
|
|
|
|
|