Regex except problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
trobale
Forum Newbie
Posts: 24
Joined: Mon Apr 23, 2007 11:42 am

Regex except problem

Post 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").
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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 */ }
trobale
Forum Newbie
Posts: 24
Joined: Mon Apr 23, 2007 11:42 am

Post 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..)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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'?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you tried adding "?" after the "*" in your tested pattern?
trobale
Forum Newbie
Posts: 24
Joined: Mon Apr 23, 2007 11:42 am

Post 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:

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Try it. You may be surprised.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, in the case of using <>, you could try [^<>].
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
trobale
Forum Newbie
Posts: 24
Joined: Mon Apr 23, 2007 11:42 am

Post by trobale »

Thank you! Problem solved...
Post Reply