如果&符号尚未编码,该如何编码? [英] How to encode the ampersand if it is not already encoded?

查看:67
本文介绍了如果&符号尚未编码,该如何编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果&号尚未被编码或不是另一个编码的epxression的一部分,我需要使用ac#方法对其进行编码

I need a c# method to encode ampersands if they are not already encoded or part of another encoded epxression

例如

"tom & jill" should become "tom & jill"


"tom & jill" should remain "tom & jill"


"tom € jill" should remain "tom € jill"


"tom <&> jill" should become "tom <&amp;> jill"


"tom &quot;&&quot; jill" should become "tom &quot;&amp;&quot; jill"


推荐答案

这应该做得很好:

text = Regex.Replace(text, @"
    # Match & that is not part of an HTML entity.
    &                  # Match literal &.
    (?!                # But only if it is NOT...
      \w+;             # an alphanumeric entity,
    | \#[0-9]+;        # or a decimal entity,
    | \#x[0-9A-F]+;    # or a hexadecimal entity.
    )                  # End negative lookahead.", 
    "&amp;",
    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

这篇关于如果&符号尚未编码,该如何编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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