0

Hi guys trying to sort my regex out for bbcodes.

I have this at the moment for urls:

 "/\[url\=\'(.+?)\'\](.+?)\[\/url\]/is", 

That will only get urls already encased in '', how can i get it to check for urls with and without surrounding single quotes?

1
  • 1
    Which language would this regular expression be used in? Commented Nov 28, 2012 at 1:41

2 Answers 2

1

You can use an alternation of patterns, the construct that uses the vertical bar (|).

"/\[url\=(\'(.+?)\'|([^'].+?))\](.+?)\[\/url\]/is", 
Sign up to request clarification or add additional context in comments.

Comments

1

You could use this regex:

\[url=(?:'([^']+)'|"([^"]+)")\](.+?)\[\/url\] 

It accepts single quote, double quote and no quote delimited url values. The downside is that you have to check multiple capture groups to get the URL attribute value.

Here are the capture groups:

  • Group 1: The value found if the attribute is single quote delimited.
  • Group 2: The value found if the attribute is double quote delimited.
  • Group 3: The text between the URL tags.

If you don't allow square brackets to appear between the URL tags, you could use the following regex instead. It will find a match more quickly in this case.

\[url=(?:'([^']+)'|"([^"]+)")\]([^\[]+)\[\/url\] 

4 Comments

When you say "If you don't allow square brackets to appear between the URL tags, you could use the following regex instead. It is better because it uses a greedy repetition character." what do you mean by that?
Also the second regex is invalid?
@LiamDawe - The greedy part ([^[]+) helps the regex match more quickly with less backtracking by consuming a whole chunk of characters until it can't anymore. A tool like RegexBuddy makes this very easy to see. Using (.+?) on the other hand consumes one character then tries to match the [, fails, backtracks, consumes another character with (.+?), tries to match the [, backtracks... until the match is found.
@LiamDawe - I re-tested the second regex and it seems to work fine, although I didn't try it in PHP. I did omit the usual forward slashes though, did you use: /[url=(?:'([^']+)'|"([^"]+)"|([^\s'"]]+))]([^[]+)[\/url]/is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.