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!
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!
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)
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)
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();
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The file name can be found in the match.Groups("file").ToStr
Bob