Reg Exp - Negating a Word seems IMPOSSIBLE! 
Author Message
 Reg Exp - Negating a Word seems IMPOSSIBLE!

I'm trying to get write a reg exp that finds a match for each word in
a sentence, excluding the ones I specify.
e.g.
My input string is "hello my name is owen"
I want to exclude the word 'name'

So the resulting matches would be:
hello
my
is
owen

I can't belive there is no way to do this!
Please help,
Owen.



Mon, 17 Jan 2005 01:49:20 GMT  
 Reg Exp - Negating a Word seems IMPOSSIBLE!

Quote:

> I'm trying to get write a reg exp that finds a match for each word in
> a sentence, excluding the ones I specify.
> e.g.
> My input string is "hello my name is owen"
> I want to exclude the word 'name'

> So the resulting matches would be:
> hello
> my
> is
> owen

> I can't belive there is no way to do this!
> Please help,
> Owen.

I'm sure there are probably easier ways than this:

foreach(split(/\s+/, "hello my name is owen")) {
   (! /^name$/) && print "$_\n";

Quote:
}

-S


Mon, 17 Jan 2005 07:18:44 GMT  
 Reg Exp - Negating a Word seems IMPOSSIBLE!

Quote:

> I'm trying to get write a reg exp that finds a match for each word in
> a sentence, excluding the ones I specify.
> e.g.
> My input string is "hello my name is owen"
> I want to exclude the word 'name'

> So the resulting matches would be:
> hello
> my
> is
> owen

/\b(?!name\b)(\w+)/g

Quote:
> I can't belive there is no way to do this!

The fact that it is possible does not mean this is the right way to do it.

This newsgroup does not exist.  Please do not start threads here.  See FAQ.



Wed, 19 Jan 2005 00:59:18 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. regex negating a search/word

2. RegEx and Negated Words

3. Store reg exp in variable?

4. reg - exp - cutting part of a string out

5. reg.exp to identify sigs in mails

6. reg exp

7. ether net address reg-exp answer summary

8. another reg exp question

9. Reg exp question

10. qr, compiling Reg. Exp under Perl 5.003

11. Reg-exp functions

12. Counting Reg Exp matches in a string

 

 
Powered by phpBB® Forum Software