General Community > Scripting Help
how do you say NOT in regex?
Aquilo:
I just tested this and it works!
//$string = 'This is not text'; //no match
$string = 'This is text'; //match
echo preg_match('~^(this is text)~i', $string) ? 'match' : ' no match';
Anguz:
I see... I've read the manual, but I don't know if it'd work... let me put an example
lets say I want to match a string that doesn't contain the substring "optional" from these two strings:
"The first part matches an optional opening parenthesis."
"The first part matches an opening parenthesis."
how would it be written?
Anguz:
--- Quote from: Aquilo on October 28, 2003, 02:57:09 PM ---I just tested this and it works!
//$string = 'This is not text'; //no match
$string = 'This is text'; //match
echo preg_match('~^(this is text)~i', $string) ? 'match' : ' no match';
--- End quote ---
hmm... then it'd be written like this?
--- Code: ---'~[\w.]*^(unwantedstring)[\w.]*~i'
--- End code ---
Aquilo:
or this!
--- Code: ---<?php
$string1 = "The first part matches an optional opening parenthesis.";
$string2 = "The first part matches an opening parenthesis.";
echo 'string one: ', preg_match('~^.*(optional).*~i', $string1) ? 'match' : ' no match';
echo '<br>'; // WHY DOES "< ? php" MESS WITH BREAK < br / >??
echo 'string two: ', preg_match('~^.*(optional).*~i', $string2) ? 'match' : ' no match';
?>
--- End code ---
or this ;D
--- Code: ---<?php
$string1 = "The first part matches an optional opening parenthesis.";
$string2 = "The first part matches an opening parenthesis.";
echo 'string one: ', preg_match('~^.*(optional).*~i', $string1) ? 'match' : ' no match',
'<br>', // WHY DOES "< ? php" MESS WITH BREAK < br / >??
'string two: ', preg_match('~^.*(optional).*~i', $string2) ? 'match' : ' no match';
?>
--- End code ---
[Unknown]:
this(?!not)
Will match "this" but not "thisnot".
(?<!not)this
Will match "this" but not "notthis".
-[Unknown]
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version