Regular Expression Question

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
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top