Link to home
Start Free TrialLog in
Avatar of jasont09
jasont09

asked on

Change Regular Expression To Find Image Src Then Change Folder Name

Hi

I'm hoping someone can show me how to change the attached code, so that I can find the exact image i'm looking for on my page.

The current code just finds the 1st image within a blog post,
 string pattern = @"<img(.|\n)+?>";

But I want it to find this image path
src='/appliances/products/17617.jpg'

the 17617 is the productid so I guess the expression will match
src='/appliances/products/*.jpg'


In the getSrc(string input) part of the code, I some how need to change the folder value for the image from
src='/appliances/products/
to
src='/appliances/sproducts/

because sproducts serves my thumbnails

Really appreciate the help.

public string getImage(bool ShowExcerpt, string input)
        {

            if (!ShowExcerpt || input == null)
                return "";

            string pain = input;
            string pattern = @"<img(.|\n)+?>";

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, pattern,

            System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);

            if (m.Success)
            {
                string src = getSrc(m.Value);
                string img = string.Format("<img width='100' height='100px' class='left' align='left'  {0}  />", src);
                return img;
            }
            else
            {
                return "";
            }
        }



        string getSrc(string input)
        {
            string pattern = "src=[\'|\"](.+?)[\'|\"]";

            System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern,
            System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);

            System.Text.RegularExpressions.Match mImg = reImg.Match(input);

            if (mImg.Success)
            {
                return mImg.Value;
            }

            return "";
        }

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

I wonder if you really need regex for this. Wouldn't a simple string search suffice?

input = input.Replace("/appliances/products/", "/appliances/sproducts/");

Open in new window

...and if you wanted the exact image:

input = input.Replace("/appliances/products/17617.jpg", "/appliances/sproducts/17617.jpg");

Open in new window

Avatar of jasont09
jasont09

ASKER

Thanks kaufmed

But without seeming to stupid, I Dont know how to implement what you have said above. Also the image name itself will be different for every page that loads

If you do want a solution like your original one, you could try this - capturing the src attribute with your original match:
public string getImage(bool ShowExcerpt, string input)
        {

            if (!ShowExcerpt || input == null)
                return "";

            string pain = input;
            string pattern = @"<img[\s\S]+?(?<source>src=['|""](.+?)['|""])[\s\S]*?>";

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, pattern,
                System.Text.RegularExpressions.RegexOptions.IgnoreCase | 
                System.Text.RegularExpressions.RegexOptions.Multiline |
                System.Text.RegularExpressions.RegexOptions.ExplicitCapture);

            if (m.Success)
            {
                string src = m.Groups["source"].ToString();
                src=src.Replace("appliances/products","appliances/sproducts");
                string img = string.Format("<img width='100' height='100px' class='left' align='left'  {0}  />", src);
                return img;
            }
            else
            {
                return "";
            }
        }

Open in new window

Also the image name itself will be different for every page that loads
Ah...  then regex makes sense  = )

Try this modification to your first function:

public string getImage(bool ShowExcerpt, string input)
{

    if (!ShowExcerpt || input == null)
        return "";

    string pain = input;
    string pattern = @"(?i)<img (?=.*?(src=['"]?/appliances/products/\\d+\.jpg))";

    System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, pattern);

    if (m.Success)
    {
        string src = m.Groups[1].Value.Replace("/products/", "/sproducts/");
        string img = string.Format("<img width='100' height='100px' class='left' align='left'  {0}  />", src);
        return img;
    }
    else
    {
        return "";
    }
}

Open in new window


Please note:  I've removed the call to getSrc--the logic that corrects the source is a combination of the pattern and line 14.
Correction to line 14:

string pattern = @"(?i)<img (?=.*?(src=['"]?/appliances/products/\\d+\.jpg['"]?))";

Open in new window

Grrrr....   One more correction...  this time, for reliability   = )

string pattern = @"(?i)<img (?=[^>]*?(src=['"]?/appliances/products/\\d+\.jpg['"]?))";

Open in new window

hi

I get red highlighted errors on this code in visual studio

string pattern = @"(?i)<img (?=[^>]*?(src=['"]?/appliances/products/\\d+\.jpg['"]?))";

src=['"]   at the end of this I get expected ;

/appliances/  syntax error, ':' expected

\\d+\  ;expected

andrew your code is working... but does this regex always fetch the image path I ask for?
I think so - it picks out the src= attribute from the img tab, which I though was what you wanted.
I really want to ensure i get the correct path

src='/appliances/products/

cos otherwise it will find the 1st image path on the blog post

OK - change my pattern string to
            string pattern = @"<img[\s\S]+?(?<source>src=['|""](/appliances/products/.+?)['|""])[\s\S]*?>";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland 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
I get red highlighted errors on this code in visual studio
I missed that you had the leading @ on your string literal. Change the "\\d" to "\d" (i.e.  a single backslash).
Thanks Andrew, This works as I needed it to.

Also thank you to kaufmed for your time and effort.