Negating a regular expression.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Negating a regular expression.

Post by onion2k »

I have a regex that will match any line that has an @ in it: .+?@.+

How do I change that to match any line that doesn't have an @ sign?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

if (preg_match("/.+?@.+/i", $s, $m) == 0)
 {
        //
 }
I think [^@] is used for negation - but dont know how or where to insert it.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I looked into that, but like you I can't quite work out how to put it into an expression to match the whole line if that bit matchs.

Really annoying.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

If I get the problem right...

you could just use

Code: Select all

if (!strstr($line,'@')) {
 //line without @
}
PHP Manual
"Tip: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster."
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

This isn't a PHP issue though. It has to be a regular expression coz I'm doing this in Textpad.

Besides, it's an interesting challenge now..
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Code: Select all

if (preg_match("/[^@]*/i", $s, $m) == 0) 
{ 
        // 
}
The above matches in between @ signs... if you wanted to match "lines" maybe:

Code: Select all

if (preg_match("/\n[^@|\n]*\n/i", $s, $m) == 0) 
{ 
        // 
}
Which says starts with a newline, ends with a new line, with no @ or newlines in between. But would miss the first and last lines.
Last edited by shoebappa on Tue Aug 16, 2005 7:11 am, edited 2 times in total.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Could *[^@]* be better ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

^[^@]+$
tested in Textpad 4.7.3
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

The above would only work if the entire string has no @ sign... If you wanted lines I think you'd have to do something like what I edited above. Although I don't use textpad, maybe it works on lines.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Textpad regex works off of lines, not entire files. (automatic multi-line mode)
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Sweet
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Yayfax! Cheers everyone.
Post Reply