使用正则表达式查找控制字符的 XML 字符引用

发布于 2024-12-04 19:50:15 字数 338 浏览 6 评论 0原文

我需要一些帮助来找出控制字符(十进制或十六进制)的 XML 字符引用的正则表达式。

这些序列如下所示:







换句话说,它们是一个 & 符号,后跟一个井号,后跟一个可选的“x”以表示十六进制模式,后跟 1 到 4 个十进制(或十六进制)数字,最后跟一个分号。

我特别尝试识别那些包含(包含)十进制 0 到 31 或十六进制 0 到 1F 的数字的序列。

有人能找出这个的正则表达式吗?

I need some help figuring out the regex for XML character references to control characters, in decimal or hex.

These sequences look like the following:







In other words, they are an ampersand, followed by a pound, followed by an optional 'x' to denote hexadecimal mode, followed by 1 to 4 decimal (or hexadecimal) digits, followed by a semicolon.

I'm specifically trying to identify those sequences where they contain (inclusive) numbers from decimal 0 to 31, or hexadecimal 0 to 1F.

Can anyone figure out the regex for this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-小熊_ 2024-12-11 19:50:15

如果您使用 零宽度先行断言 来限制位数,您可以编写模式的其余部分,无需担心长度限制。试试这个:

&#(?=x?[0-9A-Fa-f]{1,4})0*([12]?\d|3[01]|x0*1?[0-9A-Fa-f]);

说明:

(?=x?[0-9A-Fa-f]{1,4})  #Restricts the numeric portion to at most four digits, including leading zeroes.
0*                      #Consumes leading zeroes if there is no x.
[12]?\d                 #Allows decimal numbers 0 - 29, inclusive.
3[01]                   #Allows decimal 30 or 31.
x0*1?[0-9A-Fa-f]        #Allows hexadecimal 0 - 1F, inclusive, regardless of case or leading zeroes.

此模式允许在 x 后添加前导零,但 (?=x?[0-9A-Fa-f]{1,4})部分防止它们在x之前发生。

If you use a zero-width lookahead assertion to restrict the number of digits, you can write the rest of the pattern without worrying about the length restriction. Try this:

&#(?=x?[0-9A-Fa-f]{1,4})0*([12]?\d|3[01]|x0*1?[0-9A-Fa-f]);

Explanation:

(?=x?[0-9A-Fa-f]{1,4})  #Restricts the numeric portion to at most four digits, including leading zeroes.
0*                      #Consumes leading zeroes if there is no x.
[12]?\d                 #Allows decimal numbers 0 - 29, inclusive.
3[01]                   #Allows decimal 30 or 31.
x0*1?[0-9A-Fa-f]        #Allows hexadecimal 0 - 1F, inclusive, regardless of case or leading zeroes.

This pattern allows leading zeroes after the x, but the (?=x?[0-9A-Fa-f]{1,4}) part prevents them from occurring before an x.

一抹微笑 2024-12-11 19:50:15
&#(0{0,2}[1-2]\d|000\d|0{0,2}3[01]|x0{0,2}[01][0-9A-Fa-f]);

它不是最优雅的,但它应该可以工作。

在 RegexBuddy 中验证。

结果

&#(0{0,2}[1-2]\d|000\d|0{0,2}3[01]|x0{0,2}[01][0-9A-Fa-f]);

It's not the most elegant, but it should work.

Verified in RegexBuddy.

results

时间你老了 2024-12-11 19:50:15

我认为以下应该有效:

&#(?:x0{0,2}[01]?[0-9a-fA-F]|0{0,2}(?:[012]?[0-9]|3[01]));

这是一个红宝石:
http://www.rubular.com/r/VEYx25Fdpj

I think the following should work:

&#(?:x0{0,2}[01]?[0-9a-fA-F]|0{0,2}(?:[012]?[0-9]|3[01]));

Here is a Rubular:
http://www.rubular.com/r/VEYx25Fdpj

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文