Negating a string in a character class...?

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

Moderator: General Moderators

Post Reply
Fuchur
Forum Newbie
Posts: 1
Joined: Mon Sep 19, 2011 6:04 am

Negating a string in a character class...?

Post 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*
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Dione
Forum Newbie
Posts: 1
Joined: Wed Dec 14, 2011 4:33 pm

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

Post 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.
Last edited by Dione on Thu Dec 22, 2011 10:27 am, edited 1 time in total.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

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

Post 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]
Post Reply