views:

667

answers:

3

I need a regex to run against strings like the one below that will convert absolute paths to relative paths under certain conditions.

<p>This website is <strong>really great</strong> and people love it <img alt="" src="http://localhost:1379/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif" /></p>

Rules:

  • If the url contains "/Content/" I would like to get the relative path

  • If the url does not contain "/Content/", it is an external file, and the absolute path should remain

Regex unfortunatley is not my forte, and this is too advanced for me at this point. If anyone can offer some tips I'd appreciate it.

Thanks in advance.

UPDATE: To answer questions in the comments:

  • At the time the Regex is applied, All urls will begin with "http://"
  • This should be applied to the src attribute of both img and a tags, not to text outside of tags.
+1  A: 

Unless I'm missing the point here, if you replace

^(.*)([C|c]ontent.*)

With

/$2

You will end up with

/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif

This will only happen id "content" is found, so in cae you have a URL such as:

http://localhost:1379/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif

Nothing will be replaced

Hope it helps, and that i didn't miss anything.

UPDATE

Obviously considering you are using an HTML parser to find the URL inside the a href (which you should in case you're not :-))

Cheers

Marcos Placona
A: 

That is for perl, I do not know c#:

s@(<(img|a)\s[^>]*?\s(src|href)=)(["'])http://[^'"]*?(/Content/[^'"]*?)\4@$1$4$5@g

If c# has perl-like regex it will be easy to port.

ZyX
Overly complex.
Nick Presta
Definitely much more efficient ways to do this.
Matt H
You could write simpler (assuming author does not use HTML parser)?
ZyX
+4  A: 

You should consider using the Uri.MakeRelativeUri method - your current algorithm depends on external files never containing "/Content/" in their path, which seems risky to me. MakeRelativeUri will determine whether a relative path can be made from the current Uri to the src or href regardless of changes you or the external file store make down the road.

jball
This question is really not a job for regex. The method in this answer will give significantly more reliable results, and be easier to write and understand to boot.
Ipsquiggle
I appreciate the advice here. I was unaware of this method and completely agree with the risk involved in a regex solution. I will check this out and most likely accept this answer.
splatto
Accepted. Thank you
splatto