【问题标题】:Regex to match and remove comma (,) inside a tag [closed]正则表达式匹配和删除标签内的逗号 (,) [关闭]
【发布时间】:2013-08-30 05:45:28
【问题描述】:
<IndustryCapab,ilityDescr>RES</IndustryCapabilityDescr><IndustryS,pecialtyDescr>RES-Cross Industry</IndustrySpecialt,yDescr>

想要的结果:

</CapabilityDescr><IndustryCapabilityDescr>RES</IndustryCapabilityDescr><IndustrySpecialtyDescr>RES-Cross Industry</IndustrySpecialtyDescr>

【问题讨论】:

  • 到目前为止,您尝试了什么?您对可能对您有所帮助的正则表达式进行了哪些研究?

标签: .net regex vb.net


【解决方案1】:
resultString = Regex.Replace(subjectString, @"\,(?=[^<>]*>)", "");

详情

\, # 匹配逗号

(?= # 仅当以下正则表达式可以在当前位置匹配时:

[^&lt;&gt;]* # - 除了

之外的零个或多个字符
 #  - followed by a >

)#前瞻断言结束

【讨论】:

  • 逗号不是元字符,所以可以轻松写成,
【解决方案2】:

试试这个(在 C# 中)

var s = "<IndustryCapab,ilityDescr>RES</IndustryCapabilityDescr><IndustryS,pecialtyDescr>RES-Cross Industry</IndustrySpecialt,yDescr>";
var sNew = Regex.Replace(s, @"(?<=</?\w+),(?=\w+>)","");

我们正在使用积极的前瞻和后瞻来整理周围的所有单词和标签,并且只查找逗号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多