Regex except problem
Moderator: General Moderators
Regex except problem
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").
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").
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Well, a bit less of a brain rattling solution would be:
Code: Select all
$str = str_replace('key', '', $str);
if($str != '') { /* Have fun */ }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..)
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..)
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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:
Code: Select all
<ref>.*<\/ref>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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Use a lookbehind or lookahead, or group your characters..
any of those should work.
Code: Select all
/<ref>[^(ref)]*?<\/ref>/
/<ref>(?!ref).*?<\/ref>/
/<ref>.*?(?<!ref)<\/ref>/