Regular expressions that match the IMG tag and the SRC of IMG __ Regular Expressions

Source: Internet
Author: User
Tags regular expression

Text in the IMG tag:


Regular expression matching an IMG tag: string Pattern = @ "]*/s*src/s*=/s* ([']?) (? <url>/s+) '? [^>]*> ";

Regular expression that matches the src of img: string Pattern = @ "(? <=src/s*=/s*[/'/" "]?) (? <url>[http/:////]? [^'""]+)";

Matching results: http://www.mycreative.com.cn/upload/2010/4/tiancheng2.jpg

Get the SRC address of img in text:

String Pattern = @ "(? <=src/s*=/s*[/'/" "]?) (? <url>[http/:////]? [^'""]+)";

Regex reg = new Regex (Pattern, regexoptions.ignorecase);
MatchCollection Colmatch = Reg. Matches (text);

StringBuilder strimage = new StringBuilder ();
if (Colmatch.count > 0)
{
foreach (Match m in Colmatch)
{
Strimage.append (M.value). Append (', ');

}
}

Match the text of the IMG and add a link to the IMG tag

String Pattern = @ "]*/s*src/s*=/s* ([']?) (? <url>/s+) '? [^>]*> ";

Regex reg = new Regex (Pattern, regexoptions.ignorecase);
MatchEvaluator myevaluator=new MatchEvaluator (Repleacereg);
String content = Reg. Replace (text), myevaluator);

Repleacereg function:

private String Repleacereg (Match m)
{
String result=m.tostring ();

if (albumid! =-1)
{

           result= "<a href=ImageShow.aspx?albumid= "+ albumID +" > "+ M +" </a> ";
     }
      return result;
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.