Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Change Regular Expression To Find Image Src Then Change Folder Name

Posted on 2011-10-06
15
Medium Priority
?
210 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:jasont09
  • 6
  • 5
  • 4
15 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36924328
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36924333
...and if you wanted the exact image:

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

Open in new window

0
 

Author Comment

by:jasont09
ID: 36924382
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Expert Comment

by:andrewssd3
ID: 36924441
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36924487
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36924498
Correction to line 14:

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

Open in new window

0
 
LVL 75

Expert Comment

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

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

Open in new window

0
 

Author Comment

by:jasont09
ID: 36924564
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
 

Author Comment

by:jasont09
ID: 36924580
andrew your code is working... but does this regex always fetch the image path I ask for?
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36924741
I think so - it picks out the src= attribute from the img tab, which I though was what you wanted.
0
 

Author Comment

by:jasont09
ID: 36924836
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
 
LVL 17

Expert Comment

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

Open in new window

0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 2000 total points
ID: 36924876
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
 
LVL 75

Expert Comment

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

Author Closing Comment

by:jasont09
ID: 36924922
Thanks Andrew, This works as I needed it to.

Also thank you to kaufmed for your time and effort.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

580 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