内容如下:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<img height="257" src="http://www.xxx.com/img/2012/06/18/892185677421.jpg" width="247" border="0">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
<img height="257" src="http://www.xxx.com/img/2012/06/19/896585677400.jpg" width="247" border="0">cccccccccccccccccccccc
<img height="257" src="http://www.xxx.com/img/2012/06/20/896581237526.jpg" width="190" border="0">
ddddddddddddddddddddddddddddd要求如下:
找到所有img的图片如果图片宽度大于200的就设置成200,并把高度去掉,小的200的就保持不变替换后的效果如下:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<img src="http://www.xxx.com/img/2012/06/18/892185677421.jpg" width="200" border="0">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
<img src="http://www.xxx.com/img/2012/06/19/896585677400.jpg" width="200" border="0">cccccccccccccccccccccc
<img height="257" src="http://www.xxx.com/img/2012/06/20/896581237526.jpg" width="190" border="0">
ddddddddddddddddddddddddddddd
非常感谢大家的帮助,我想了好久想不出来。
注:不能用JS

解决方案 »

  1.   

    yourhtml=Regex.Replace(yourhtml,@"(?<=<img\b[^>]*?width=(['""]?))\d+(?=\1)",
    m=>int.Parse(m.Value)>=200?"200":m.Value);
      

  2.   


            private string CapText(Match m)
            {
                if (Convert.ToInt32(m.Value) > 200)
                    return "200";
                else
                    return m.Value;
            }        private void button10_Click(object sender, EventArgs e)
            {
                StreamReader reader = new StreamReader("c:\\1.txt",Encoding.Default);
                string source = reader.ReadToEnd();
                Regex reg = new Regex(@"(?is)(?<=<img[^>]*?width="").*?(?="" border=""0"">)");
                source = reg.Replace(source, new MatchEvaluator(CapText));
            }
      

  3.   


    谢谢,这样宽带确实是可以,但是必须要把高度height 去掉,要不然图片就会变形了
      

  4.   

        string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
                    string pattern = @"(<img[^>]*?)(height=(['""])\d+\3)([^>]*?width=(['""]))(\d+)\5([^>]*?>)";
                    
                    tempStr = Regex.Replace(tempStr, pattern, a =>
                    {
                        if (int.Parse(a.Groups[6].Value) > 200)
                        {
                            return a.Groups[1].Value + a.Groups[4].Value + "200" + a.Groups[7].Value;
                        }
                        else
                            return a.Value;
                    });
      

  5.   

    这个如何把height 去掉,那就完美了
      

  6.   

    那就来个去height的版本吧:yourhtml=Regex.Replace(yourhtml,@"(?i)(<img\b[^>]*?)height="\d+"([^>]*?)(width=)"(?:[2-9]\d{2}|\d{4})"([^>]*?>)","$1$2$3\"200\"$4");
      

  7.   


    Regex.Replace(
    原字符串,
    @"(?is)(?<=<img[^>]*)(\s*height="")(\d+)("")([^>]+?)(\s*width="")(\d+)("")(?=[^>]*>)",
    m =>
    {
    return
    int.Parse(m.Groups[6].Value) > 200
    ? m.Groups[4].Value + m.Groups[5].Value + "200" + m.Groups[7].Value
    : m.Value;
    }
    );
      

  8.   


    可惜,差点,我改改yourhtml=Regex.Replace(yourhtml,@"(?i)(<img\b[^>]*?)(width=)""(?:[2-9]\d{2,}|\d{4,})""([^>]*?>)","$1$2$3\"200\"$4");
    yourhtml=Regex.Replace(yourhtml,@"height="\d+"\s*","");
    就可以了。
      

  9.   


    可惜,差点,我改改,改成下面的这个(不知道是不是发重了,发重了,看下面这条。)。yourhtml=Regex.Replace(yourhtml,@"(?i)(<img\b[^>]*?)(width=)""(?:[2-9]\d{2,}|\d{4,})""([^>]*?>)","$1$2\"200\"$3");
    yourhtml=Regex.Replace(yourhtml,@"height="\d+"\s*","");
    就可以了。
      

  10.   

    yourhtml=Regex.Replace(yourhtml,@"(?i)(<img\b[^>]*?)(width=)""(?:[2-9]\d{2,}|\d{4,})""([^>]*?>)","$1$2\"200\"$3");
    去掉height的,改超过200的宽的图片为200,yourhtml=Regex.Replace(yourhtml,@"height="\d+"\s*","");
    去掉宽没有超过200的图片的height
      

  11.   


    你的这个正则是匹配不到width小于200的图片,因此你也就去不了,宽小于200图片的高!
    如果没这个问题,我倒是很想看看一个正则怎么搞定,请多指教。
      

  12.   


                string str = @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    <img height=""257"" src=""http://www.xxx.com/img/2012/06/18/892185677421.jpg"" width=""247"" border=""0"">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    <img height=""257"" src=""http://www.xxx.com/img/2012/06/19/896585677400.jpg"" width=""247"" border=""0"">cccccccccccccccccccccc
    <img height=""257"" src=""http://www.xxx.com/img/2012/06/20/896581237526.jpg"" width=""190"" border=""0"">
    ddddddddddddddddddddddddddddd";
                string result = Regex.Replace(str, @"(?<=<img[^>]*?)height=""[^""]+""([^>]*?)width=""([^""]+)""(?=[^>]*?>)", delegate(Match m)
                {
                    return int.Parse(m.Groups[2].Value) > 200 ? m.Groups[1].Value + "width=\"200\"" : m.Value;
                });
                Console.WriteLine(result);
    /*
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    <img  src="http://www.xxx.com/img/2012/06/18/892185677421.jpg" width="200" borde
    r="0">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    <img  src="http://www.xxx.com/img/2012/06/19/896585677400.jpg" width="200" borde
    r="0">cccccccccccccccccccccc
    <img height="257" src="http://www.xxx.com/img/2012/06/20/896581237526.jpg" width
    ="190" border="0">
    ddddddddddddddddddddddddddddd
    */
      

  13.   


    看清楚楼主的要求哦所以width小于200的根本没必要去匹配 
      

  14.   

    看到督察写的,想到可以直接查出width>200的替换
                string str = @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    <img height=""257"" src=""http://www.xxx.com/img/2012/06/18/892185677421.jpg"" width=""247"" border=""0"">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    <img height=""257"" src=""http://www.xxx.com/img/2012/06/19/896585677400.jpg"" width=""247"" border=""0"">cccccccccccccccccccccc
    <img height=""257"" src=""http://www.xxx.com/img/2012/06/20/896581237526.jpg"" width=""190"" border=""0"">
    ddddddddddddddddddddddddddddd";
                string result = Regex.Replace(str, @"(?<=<img[^>]*?)height=""[^""]+""([^>]*?)width=""[2-9]\d{2,}""(?=[^>]*?>)", "$1width=\"200\"");
                Console.WriteLine(result);
      

  15.   

    using System.Text.RegularExpressions;
    namespace img
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string regex = "<img( height=\"257\") ([\\s\\S]+?)\" width=\"(\\d+)\" border=\"0\">";
            string str1 = "<img height=\"257\" src=\"http://www.xxx.com/img/2012/06/18/892185677421.jpg\" width=\"247\" border=\"0\">";
            string str2 = "<img height=\"257\" src=\"http://www.xxx.com/img/2012/06/18/892185677421.jpg\" width=\"20\" border=\"0\">";        public string GetImg(string regex, string Text)
            {
                string str = "<img ";
                Regex rg = new Regex(regex);
                MatchCollection mc = rg.Matches(Text);
                foreach (Match m in mc)
                {
                    if (int.Parse(m.Groups[3].Value) > 200)
                        str += m.Groups[2].Value.ToString() + "\" width=\"200\" border=\"0\">";
                    else
                        str = Text;
                }
                return str;
            }        private void button1_Click(object sender, EventArgs e)
            {
                str1 = GetImg(regex, str1);
                label1.Text = str1;
                str2 = GetImg(regex, str2);
                label2.Text = str2;        }慢了点啊
      

  16.   

    谢谢各位现在假如有一个图片<img src="http://www.xxx.com/img/2012/06/18/957812335695.jpg" width="650" height="108">其中width 和height 的值不确定是否有带"" 引号
    即如:
    width="650" 或 width=650这时用正侧如何去掉width 和height 效果:
    <img src="http://www.xxx.com/img/2012/06/18/957812335695.jpg">
      

  17.   

    Try:yourhtml=Regex.Replace(yourhtml,@"(?i)(<img\b[^>]*?)height=(['"]?)\d+\2([^>]*?)
    (width=)(['"]?)(?:[2-9]\d{2}|\d{4,})\5([^>]*?>)","$1$3$4$2 200$2$6");
      

  18.   

                string str = @"<img src=""http://www.xxx.com/img/2012/06/18/957812335695.jpg"" width=""650"" height=""108"">
    <img src=""http://www.xxx.com/img/2012/06/18/957812335695.jpg"" width=650 height=108>
    <img src=""http://www.xxx.com/img/2012/06/18/957812335695.jpg"" width='650' height='108'>";
                string result = Regex.Replace(str, @"(?i)(?<=<img\b[^>]*?)(?:width|height)=(['""]?)[^'""\s>]+\1(?=[^>]*?>)", "");
                Console.WriteLine(result);
      

  19.   

    Try:yourhtml=Regex.Replace(yourhtml,@"(?i)(<img\b[^>]*?)(width=)(['""]?)(?:[2-9]\d{2}|\d{4,})\3\sheight=(['""]?)\d+\4([^>]*?>)","$1$5");