无法制作有效的正则表达式

时间:2017-07-18 05:44:37

标签: c# regex

我是Regex的新手,几天前开始玩它。但现在我被困在一个string

例如: 我有这个字符串 - &gt; <a href="http://somelink.com", example1="", example2="">

我正在尝试Replace<a>的所有字符串,但我想保留href部分和链接。我在https://regex101.com一直在讨论这个问题,但无济于事。我正在尝试的Regex模式是<a(\s?)(?!.*?href=\".*?\").*?>。此模式在字符串中找不到任何内容。我正在使用C#

非常感谢任何帮助。感谢

更新

实际字符串看起来像

<a href="http://somelink.com", example1="", example2="">

我想删除这部分

, example1="", example2=""

但后来我想保留这部分

<a href="http://somelink.com">

2 个答案:

答案 0 :(得分:0)

这可能有效<a(?:.*?)(href=\"(.*?)\").*?>
组(1)= href="..."和组(2)= link

演示https://regex101.com/r/Yz0kPc/1
添加了更多属性https://regex101.com/r/Yz0kPc/2

答案 1 :(得分:-1)

我认为你可以搜索:

<a[^>]*?(href="[^"]+")[^>]*>

并替换为:

<a $1>

Run the source

Demo