1

I need a single regex to capture the relative path /image/picture/name.jpg from the below 3 similar strings. I tried many ways but there are some vulnerabilities in my regex code which is making it to work inconsistently. I couldn't find any perfect solution for this specific issue. Any help is Greatly appreciated.

  • string 1 : url(/image/picture/name.jpg)
  • string 2 : url("/image/picture/name.jpg")
  • string 3 : url('/image/picture/name.jpg')
1
  • 1
    There is no perfect solution for this using regular expressions (excluding a "regular expression" with recursion/back-refs/code-expressions, and even then it's debatable on context). Some of the answers below "will work", but keep in mind it is only over the limited test-data provided. Commented May 3, 2011 at 20:01

5 Answers 5

2

This should capture the URL into group 1:

url\(((?:"|')?)([a-z/\.]*?)\1\)

It will also capture anything else within the URL tag's brackets that looks like a URL but is nice and specific to the case...

Sign up to request clarification or add additional context in comments.

2 Comments

This will catch url("foo')wrong") Would +1 if using a back-reference, although that is still not perfect.
Fixed it up and added back references :)
2

This will catch the cases you mention, though it's a little on the expensive side:

(?ix-:url\(\s*
(?:
  (?<url>[^\)'"][^\)]*[^\)\s]) |
  (?:'(?<url>[^']*)') |
  (?:"(?<url>[^"]*)")
)\s*
\))

Rather I would use the following and then test for and trim the quotes manually:

url\(\s*(?<url>[^)'"][^\)]*[^\)\s])\s*\)

The down-side to the later expression is that it does not correctly handle the quoted close parenthesis ')' character as in the following example:

String: url( '/images/logo Copy(2).jpg' )

Comments

0

Try this:

url(("|')?/image/picture/name.jpg("|')?)

1 Comment

This will catch url("foo')wrong") Would +1 if using a back-reference, although that is still not perfect.
0

If I understand you correctly and you want everything inside the parentheses but not quotes to be included the following statement should do. It will of course not accept numbers etc, but will return enything from the first slash encountered until the last character not a slash or alpha-character.

Regex re = new Regex(@"/([a-z/.])*", RegexOptions.IgnoreCase);

Comments

0

Try this:

url\(['"]?(?<url>[^'")])['"]?\)

The exact url could be matched in group named "url"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.