Regex question

Posted on 2005-05-09
Last Modified: 2010-04-16
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.

Question by:RoninThe
    LVL 96

    Expert Comment

    by:Bob Learned
    (<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.


    LVL 17

    Expert Comment


    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);
    (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)
    LVL 5

    Author Comment

    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();
    LVL 17

    Accepted Solution

    In that case you need to use a MatchEvaluator

                public void test(string input)
                      Regex rx = new Regex("<img\\s+src=\"(?<filename>[^\"]+)\"\\s*>", RegexOptions.IgnoreCase);
                      rx.Replace(input, new MatchEvaluator(ReplacePath));

                private string ReplacePath(Match m)
                      string path = m.Groups["filename"].Value;
                      // do stuff with obtained path
                      path = "new path here";
                      return "<img\\s+src=\"" + path + "\"\\s*>"";

    This will trigger the ReplacePath function for each and every match that's found. It allows you to do anything you want in the function. at the end you must return a string in the MatchEvaluator function. This return value will replace the original text of the match.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now