-2

Here the example.

Starts with : imgurl= Ends with : &amp

Example extraction

asfasfasfasimgurl=http://www.mysite.com&ampasgasgas

Result: http://www.mysite.com

So how can I write regex to extract all instances like this?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • What have you tried? What if there are multiple occurrences of `imgurl=` or `$amp`? From first to last, from first to first, from last to first, from last to last? Provide some more examples and your own attempt, please. – Martin Ender Jul 03 '13 at 19:31
  • @m.buettner thanks for answer. instead of thinking too much pattern for this particular example may work for me. I am also trying myself right now. – Furkan Gözükara Jul 03 '13 at 19:32

2 Answers2

2

You can use lookbehind and lookahead

 (?<=imgurl=).*?(?=&amp)

You can get a list of urls using

 List<String> urls=Regex.Matches(input,regex)
                        .Cast<Match>()
                        .Select(x=>x.Value)
                        .ToList();
Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89
2

A simple regex could be:

(?:imgurl=)(.*)(?:&amp)

the (?:[stuff here]) is a non-capture group. It requires the pattern to match, but not capture/extract. The (.*) captures everything in-between the two non-capture groups.

Also to learn more about capture groups you can read here What is a non-capturing group? What does a question mark followed by a colon (?:) mean?

Community
  • 1
  • 1
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85