Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Aug 16, 2005 4:11 am
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?
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 16, 2005 5:00 am
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.
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Aug 16, 2005 5:40 am
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 » Tue Aug 16, 2005 5:41 am
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."
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Aug 16, 2005 6:14 am
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..
shoebappa
Forum Contributor
Posts: 158 Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA
Post
by shoebappa » Tue Aug 16, 2005 6:51 am
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.
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 16, 2005 7:00 am
Could *[^@]* be better ?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 16, 2005 7:08 am
shoebappa
Forum Contributor
Posts: 158 Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA
Post
by shoebappa » Tue Aug 16, 2005 7:13 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 16, 2005 7:15 am
Textpad regex works off of lines, not entire files. (automatic multi-line mode)
shoebappa
Forum Contributor
Posts: 158 Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA
Post
by shoebappa » Tue Aug 16, 2005 7:16 am
Sweet
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Aug 16, 2005 8:45 am
Yayfax! Cheers everyone.