Regular Expression Question

  • Thread starter Thread starter Matthias S.
  • Start date Start date
M

Matthias S.

Hi there,

I'm trying to build a regular expression which will do a replace for me. I'm
getting grey hair on this one:

here is my input string:
<a href="http://test.com/fun.jpg" target="_blank">my fun pic</a>

I'd like to turn this into the following:
http://test.com/fun.jpg

Can somebody help me building such a regular expression? Any help is greatly
appreceated. Thanks in advance!

Matthias
 
The best way I can think of it is using the following:

(?i)(?:<a[^>]*href="?){1}(?<url>[^\s>"]*)

The first expression is a mode modifier, indicating that the regex is
case-insensitive. I prefer to use these rather than setting options. The
second part is a non-capturing group that defines the beginning of a link
tag, and it specifies exactly one time. Because an HTML tag may or may not
use quotes in the attribute value, the quote prior to the URL is optional (0
or 1 time). After that there is a single group named "url" that contains the
URL you're looking for. It is defined as any number of any character that is
NOT a space, right angle bracket, or a quote. You replace the group using
the Regex.Replace override that takes a MatchEvaluator delegate.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

I had the same problem once. Fixed it using the same solution.
 
Back
Top