Regex Exclusion

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

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Regex Exclusion

Post by Benjamin »

I have been mucking with this for several hours. Is it even possible to conditionally match text?

I want to match this..

#blah anything goes here#

But I don\\\'t want it to match this..

#blah anything | goes here#
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Possible, yes. Can we give you a working pattern.. I don't think so. I believe we'll need several more examples of matching and not matching subjects.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

feyd wrote:I believe we'll need several more examples of matching and not matching subjects.
Or perhaps a definition for what you would like to allow and what is not allowed.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

based on what you've already posted

Code: Select all

/#(.*?)#/
should be all you need
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

nickvd wrote:based on what you've already posted

Code: Select all

/#(.*?)#/
should be all you need
That won't work because it will match even if there is a | between the #'s.

I already solved this issue a different way, but I'm still not clear on how to do exclusions in regex.

For example, if I want to match every thing encased in # signs, but only if it does not contain a pipe ( | ), how is this done? I've been reading regex documentation and haven't been able to figure it out.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Try this:

Code: Select all

$regex = '@#([^\|]*)#@U';
But you really need to give us a better definition so we can help better.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Hmm, that works. How does it work? I tried /#.*?[^\|].*?#/ and it didn't work. What did I do wrong?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

astions wrote:Hmm, that works. How does it work? I tried /#.*?[^\|].*?#/ and it didn't work. What did I do wrong?
First, the two .*? are useless since the [^\|] already means "any character except pipes".
Second, I used the U (PCRE_UNGREEDY) modifier - here is what the manual says about the U modifier:

U (PCRE_UNGREEDY)

This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by "?". It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).


Assuming we work with this string: just a test #catch me# and #catch me too#, without the U modifier the regex would have caught this:

catch me# and #catch me too

With the U modifier it will catch these:

1. catch me
2. catch me too

Hope that helps :wink:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ok so it was getting picked up by the .+?. I get it now. Thank you.
Post Reply