替换为正则表达式保留模式的一部分

时间:2021-10-31 21:45:33

In the example below, how to preserve part of a pattern? The pattern search must include closing tag </h3> as spans elsewhere must not be targeted.

在下面的示例中,如何保留模式的一部分?模式搜索必须包括结束标记 ,因为其他地方的跨度不能被定位。

Example: remove only </span> from </span> some text </h3>:

示例:仅删除 某些文本 :

regEx.Pattern = "</span>[^>]*</h3>"
hr2 = regEx.Replace(hr2,"[^>]*</h3>")  '<- does not work

1 个解决方案

#1


2  

You need to set a capturing group and use a back reference in the replacement pattern.

您需要设置捕获组并在替换模式中使用后引用。

regEx.Pattern = "</span>([^>]*)</h3>"
hr2 = regEx.Replace(hr2,"$1</h3>")

Or

要么

regEx.Pattern = "</span>([^>]*</h3>)"
hr2 = regEx.Replace(hr2,"$1")

#1


2  

You need to set a capturing group and use a back reference in the replacement pattern.

您需要设置捕获组并在替换模式中使用后引用。

regEx.Pattern = "</span>([^>]*)</h3>"
hr2 = regEx.Replace(hr2,"$1</h3>")

Or

要么

regEx.Pattern = "</span>([^>]*</h3>)"
hr2 = regEx.Replace(hr2,"$1")