Regular expression question : how to not match a word

L

linq936

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

J. Gleixner

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";
}

"
 
P

Paul Lalli

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
 
X

Xicheng

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
 
B

batista

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";
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top