0

This is probably a rather simple question to solve, butI'm trying to use a Regex on a string and have it remove any src attributes that contain dimensions, like this:

src="/some/path/here_000x000.jpg"

so

<img src="/some/path/here_000x000.jpg"/>

would become

<img />

after processing.

From researching I found that

\d{1,5}x\d{1,5}

finds dimensions, and

src\s*=\s*"(.+?)" 

will find a src attribute, but how can I combine these both and have some simple c# code whereby all matching patterns in a given string are removed (ie. replaced with '')?

Many thanks

4

3 回答 3

2
src\s*=\s*"[^"]*?\d+x\d+.*?"

从技术上讲,您不应该将数字限制为五位(即使不需要这样)。

这匹配src(1)、任意数量的空格 (2)、=(3)、任意数量的空格 (2)、"(4)、任意数量的任意字符(除了")(非贪婪)(5)、任意正数位数 (6)、x(7)、任意正数位数 (6)、任意字符数(换行符除外)(非贪婪地)(8) 和"(3)。

  1. src
  2. \s*
  3. =
  4. "
  5. [^"]*?
  6. \d+
  7. x
  8. .*?
于 2013-01-31T15:32:48.673 回答
2

你可以使用这个正则表达式

src\s*=\s*"[^"]*\d+x\d+[^"]*"

逃脱"_""

于 2013-01-31T15:33:17.233 回答
0

试试这个正则表达式:

src=".*?\d+x\d+\.\w+"

它将匹配 1 个或多个数字

于 2013-01-31T15:35:00.160 回答