Page 1 of 1

Negating a string in a character class...?

Posted: Mon Sep 19, 2011 6:21 am
by Fuchur
Hello :),

I have a question regarding regular expressions.

On of the strings I want to proceed:
[text]The easiest way to get this going is to use this link: PatchWork[up]3d[/up].[/text]

I used this expression before to replace, which was working, but now, since there is the [up]...[/up] statement in it, I get a problem:

Code: Select all

echo preg_replace("%\[url=([^\]]*)\]([^/[]*)\[/url\]%", "<a href=\"$1\">$2</a>");
I thought about something like this, but I cant get it do work.

Code: Select all

echo preg_replace("%\[url=([^\]]*)\]([^/[/url\]]*)\[/url\]%", "<a href=\"$1\">$2</a>");
Any ideas about that?

See you and thank you very much in advance
*Fuchur*

Re: Negating a string in a character class...?

Posted: Mon Sep 19, 2011 12:41 pm
by AbraCadaver
I'm having trouble seeing what you're trying to do, but you can't negate a string. What you are doing is just for characters. Have a look at the lookaheads and lookbehinds:

http://www.regular-expressions.info/refadv.html

Re: Negating a string in a character class...?

Posted: Wed Dec 14, 2011 4:42 pm
by Dione
Thanks for the link, this information helped me figure out my issue. I am still a little bit confused on whether I need to use a negative lookahead for a simple capture like this one though. Either way, I have seemed to resolve the issue that was preventing me from moving forward on the online payroll software that I am developing, at least for now. Hopefully I won't run into anymore coding issues like this one, and I can finish this project without any further complications. If anyone can help offer me some more clarity on the proper use of lookaheads and lookbehinds, I would be very grateful. This would probably save me from this type of snag in the future. Thanks.

Re: Negating a string in a character class...?

Posted: Thu Dec 15, 2011 2:09 pm
by ragax
For the record, you may not need a negative lookahead for this simple capture.
This regex:

Code: Select all

\[url=([^]]*)\](.*?)\[/url\]
Captures httpETC in Group 1 and MyUrl in Group 2
in

Code: Select all

[url=httpETC]MyUrl[/url]