Page 1 of 1
Regex except problem
Posted: Mon May 28, 2007 12:47 pm
by trobale
How do I write a regular expression that matches anything, except a specific string (not specific characters)?
Example:
keyjustrandomtextkey
Now I can't use regex: key[^key]*key (and I know I could use key.*key but I run regex on multiple lines and the string I regex after appears several times...)
So in the previous example I wan't to accept all characters except characters :k,e,y if they are after each other i the specific order (="key").
Posted: Mon May 28, 2007 1:08 pm
by superdezign
Well, a bit less of a brain rattling solution would be:
Code: Select all
$str = str_replace('key', '', $str);
if($str != '') { /* Have fun */ }
Posted: Mon May 28, 2007 1:30 pm
by trobale
Thanks for your answer although it didn't help me much.
In this case I wan't to completely remove the whole string: keyjustrandomtextkey
And in other cases I only wan't to remove the string "key" from the string (and keep "justrandomtext")
Therefore I have to use regular expressions and need a bit more of brain rattling (maybe I could do it with php string functions but it is much more complicated..)
Posted: Mon May 28, 2007 1:37 pm
by superdezign
trobale wrote:In this case I wan't to completely remove the whole string: keyjustrandomtextkey
I thought you meant get rid of 'key' and keep the rest. What are you trying to do then? Take everything within them, like.. between pairs of 'key'?
Posted: Mon May 28, 2007 11:09 pm
by feyd
Have you tried adding "?" after the "*" in your tested pattern?
Posted: Tue May 29, 2007 8:26 am
by trobale
If I remember right ? means zero or more occurances of the character.
My problem is that I have for example the following string:
<ref>reference</ref> Some text that I don't want to be removed<ref>another reference</ref>
And I wan't to completely remove all the "<ref>reference</ref>" things.
Since I'm running regex on multiple lines the following won't do, since it removes also the text between the refs:
Therefore I wan't to accept all characters except the string </ref>
<ref>[^<\/ref>]*<\/ref> does not work, since it doesn't treat [^<\/ref>] as a string but as characters.
Posted: Tue May 29, 2007 8:42 am
by feyd
Try it. You may be surprised.
Posted: Tue May 29, 2007 8:55 am
by superdezign
Well, in the case of using <>, you could try [^<>].
Posted: Tue May 29, 2007 9:27 am
by Jenk
Use a lookbehind or lookahead, or group your characters..
Code: Select all
/<ref>[^(ref)]*?<\/ref>/
/<ref>(?!ref).*?<\/ref>/
/<ref>.*?(?<!ref)<\/ref>/
any of those should work.
Posted: Wed May 30, 2007 2:14 am
by trobale
Thank you! Problem solved...