替换为正则表达式保留模式的一部分 [英] Replace with regex keeping part of the pattern

查看:57
本文介绍了替换为正则表达式保留模式的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中,如何保留部分模式?模式搜索必须包括结束标记 </h3>,因为其他地方的跨度不能作为目标.

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

推荐答案

您需要设置一个捕获组并在替换模式中使用反向引用.

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>")

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

这篇关于替换为正则表达式保留模式的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆