General Community > Scripting Help

how do you say NOT in regex?

<< < (2/3) > >>

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 &#39;string one: &#39;, preg_match(&#39;~^.*(optional).*~i&#39;, $string1) ? &#39;match&#39; : &#39; no match&#39;;

echo &#39;<br>&#39;; // WHY DOES "< ? php"  MESS WITH BREAK < br / >??

echo &#39;string two: &#39;, preg_match(&#39;~^.*(optional).*~i&#39;, $string2) ? &#39;match&#39; : &#39; no match&#39;;
?>
--- 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 &#39;string one: &#39;, preg_match(&#39;~^.*(optional).*~i&#39;, $string1) ? &#39;match&#39; : &#39; no match&#39;,

&#39;<br>&#39;, // WHY DOES "< ? php"  MESS WITH BREAK < br / >??

&#39;string two: &#39;, preg_match(&#39;~^.*(optional).*~i&#39;, $string2) ? &#39;match&#39; : &#39; no match&#39;;
?>
--- 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