Page 1 of 1
Negating a regular expression.
Posted: Tue Aug 16, 2005 4:11 am
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?
Posted: Tue Aug 16, 2005 5:00 am
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.
Posted: Tue Aug 16, 2005 5:40 am
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.
Posted: Tue Aug 16, 2005 5:41 am
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."
Posted: Tue Aug 16, 2005 6:14 am
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..
Posted: Tue Aug 16, 2005 6:51 am
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.
Posted: Tue Aug 16, 2005 7:00 am
by anjanesh
Could *[^@]* be better ?
Posted: Tue Aug 16, 2005 7:08 am
by feyd
Posted: Tue Aug 16, 2005 7:13 am
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.
Posted: Tue Aug 16, 2005 7:15 am
by feyd
Textpad regex works off of lines, not entire files. (automatic multi-line mode)
Posted: Tue Aug 16, 2005 7:16 am
by shoebappa
Sweet
Posted: Tue Aug 16, 2005 8:45 am
by onion2k
Yayfax! Cheers everyone.