Link to home
Start Free TrialLog in
Avatar of RoninThe
RoninThe

asked on

Regex question

hello experts,

I have one html file. I want to

1) search this file for IMG tags with only src attribute, e.g.
<IMG src="C:\Some Folder\1.jpg"> should be selected, but <IMG alt="alt img text" src="C:\Some Other Folder\11.jpg"> shouldn't be selected)
2) obtain value of the src attribute [from all the selections as in (1)] (e.g. "C:\Some Folder\1.jpg")
3) copy the image file from src location to another location, and replace the src value to relative path of that new location[for each of the selection in (1)]

regex should be the faster way to do the above tasks, otherwise I could have accomplished this with linear text processing.

thanks!
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

(<IMG src=)(?<file>)(.+)>

The file name can be found in the match.Groups("file").ToString() value.  You'll have to remove the quotes from the name.

Bob

img\s+src="(?<filename>[^"]+)"

should work better as it always returns the correct filename.

you can use

Match m = RegEx.Match(text, pattern, options);
while (m.Success)
{
      string url = m.Groups["filename"].Value;
      File file = File.Open(url, mode);
      m.NextMatch();
}
(from the top of my head, leaving some blanks, my pc just broke down, so I'm currently reinstalling everything and can't check).

If this is not enough to make things work I'll try to whip up something in VS later tonight (when everything is up and running)
Avatar of RoninThe
RoninThe

ASKER

hey Bob, i'm afraid  (<IMG src=)(?<file>)(.+)> doesn't seem to work. I changed it to (<IMG\ssrc=)(?<file>)(.+)> ,then it gave me matches but no named group- matches.

ToAoM,  your regex does work, but how would I replace the matched value with another one(#3 in que).

ps: one correction :-)
m.NextMatch(); should be m=m.NextMatch();
ASKER CERTIFIED SOLUTION
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial