-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?

4

2 回答 2

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();
于 2013-07-03T19:34:45.713 回答
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?

于 2013-07-03T19:51:16.250 回答