0

I'm trying to decode the following regex expression. For some reason, it doesn't work on my online decoder. Can anybody help me decrypt the following:

Regex r = new Regex("src=\"(?<src>[^\"]+)\"")

Thanks.

2 Answers2

4

It seems that you're missing the last quotation symbol, but let's ignore that.

Autopsy:

src=\"(?<src>[^\"]+)\"
  • src= - The literal string src=
  • \" - A literal quote (escaped to make sure the string doesn't terminate)
  • (?<src>[^\"]+)
    • A capturing group named src (?<src>) matching:
    • [^\"]+ - Any character that isn't a " character, matched 1 to infinity times (+)
  • \" - A literal quote (escaped to make sure the string doesn't terminate)

Debuggex:

Regular expression visualization

In human words:

With the string <img src="picture.png" /> this will create a named capturing group named src containing picture.png.

Regex limitations:

If your image is created using single quotes (<img src='picture.png' />) this will not work correctly. In that case you can use something like:

src=(\"(?<src1>[^\"]+)\"|\'(?<src2>[^\']+)\')
                        ^
     DOUBLE QUOTES     OR      SINGLE QUOTES

that will match both (in either src1 or src2 depending on the quotation type).

Regular expression visualization

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • thanks. added the missing " –  May 23 '16 at 18:00
  • If you're going to ignore the regex in string form, why do you escape the double qutation's ? That's not what is passed to the regex engine.. –  May 23 '16 at 20:46
0

Perhaps because your pattern was incorrect to begin with? I think this is what you wanted:

Regex r = new Regex("src=\"([^\"]+)\"")

This will capture image.jpeg in src="image.jpeg".

Regex 101

Community
  • 1
  • 1
Code Different
  • 90,614
  • 16
  • 144
  • 163