在HTML代码中,img的写法大致如下:

<img src=”…” alt=”…” width=”…” height=”…” />

我们利用正则表达式来提取 width 和 height 的值:

string str = "<img src=\"/upload/1.jpg\" width=\"100\" height=\"80\">"; 
string pattern = "width\\s?=\\s?\"(\\d+)\"\\s+height\\s?=\\s?\"(\\d+)"; 
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
System.Text.RegularExpressions.Match m = regex.Match(str); 

if (m.Success) 
{ 
    string width = m.Groups[1].Value; //宽 
    string height = m.Groups[2].Value; //高 
} 

相关文章:

  • 2021-09-28
  • 2022-01-03
  • 2021-10-13
  • 2021-08-28
  • 2021-10-04
  • 2021-11-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-02
相关资源
相似解决方案