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

jasont09Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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

0
käµfm³d 👽Commented:
...and if you wanted the exact image:

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

Open in new window

0
jasont09Author Commented:
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

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

andrewssd3Commented:
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

0
käµfm³d 👽Commented:
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.
0
käµfm³d 👽Commented:
Correction to line 14:

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

Open in new window

0
käµfm³d 👽Commented:
Grrrr....   One more correction...  this time, for reliability   = )

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

Open in new window

0
jasont09Author Commented:
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

0
jasont09Author Commented:
andrew your code is working... but does this regex always fetch the image path I ask for?
0
andrewssd3Commented:
I think so - it picks out the src= attribute from the img tab, which I though was what you wanted.
0
jasont09Author Commented:
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

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

Open in new window

0
andrewssd3Commented:
Actually looking at that again you don't need the pipes:
            string pattern = @"<img[\s\S]+?(?<source>src=['""](/appliances/products/.+?)['""])[\s\S]*?>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
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).
0
jasont09Author Commented:
Thanks Andrew, This works as I needed it to.

Also thank you to kaufmed for your time and effort.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.