非零十六进制值的正则表达式
我正在寻找一个正则表达式来确定 32 位十六进制值中的任何值何时为非零。
数据模式看起来像 0x00000000
,我想知道何时任何数字不为零。 例如,如果正则表达式捕获 0x00001000
或 0x10000000
或 0xB000000
,但不是 0x00000000
模式。 现在,我执行步行模式匹配,
0x[^0]
0x0[^0]
0x00[^0]
...
0x0000000[^0]
这将起作用,但如果可能的话,我更愿意使用一种模式。 谢谢。
标记
编辑:我没有提到,因为程序中不需要 RegEx,否则我会使用不同的方法,但我使用 RegEx 使用 UltraEdit 来搜索日志文件中的值。 我本可以开发一个程序或其他一些搜索方式,但我只是懒惰,只是说实话。 Ben S 解决方案适用于 UltraEdit 和 Rad Software 正则表达式设计器。 Rampion 解决方案在这两种工具中都不起作用,不知道为什么。
I am looking for a regular expression to determine when any of the values in a 32-bit hex value is non-zero.
The data patterns look like 0x00000000
and I want to know when any of the digits is non-zero. For example, if 0x00001000
or 0x10000000
or 0xB000000
would be capture by the regular expression, but not a 0x00000000
pattern. Right now I perform a walking pattern match of
0x[^0]
0x0[^0]
0x00[^0]
...
0x0000000[^0]
This will work, but I much rather have one pattern if possible. Thanks.
Mark
Edit: I didn't mention as the RegEx was not needed in a program, otherwise I would have used a different approach, but I was using the RegEx to search for values in a log file using UltraEdit. I could have developed a program or some other means to search, but I was just being lazy, just being honest. Ben S solution worked both in UltraEdit and Rad Software Regular Expression Designer. rampion solution didn't work in either tool, not sure why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
为什么不测试十六进制值是否为零? 更简单、更快、更具可读性。
如果确实需要正则表达式g,则应该使用
0x0*[1-9a-fA-F][0-9a-fA-F]*
。它会查找尽可能多的零,直到找到非零的十六进制值,然后收集其余的十六进制值,无论它是否为零。
注意:这将匹配任何长度的十六进制,而不仅仅是 32 位。
Why not test the hex value against zero? Simpler, faster, more readable.
If a regular expressiong is really necessary,
0x0*[1-9a-fA-F][0-9a-fA-F]*
should do it.It looks for as many zeros as it can until it finds a non-zero hex value, then gathers the rest of the hex regardless of if it's a zero or not.
Note: this will match any length hex, not just 32 bits.
*
表示匹配原子 0 次或多次,因此该模式匹配0x
前缀,后跟 0 或多个0
s,后跟一个非零十六进制,再后跟一些十六进制。<atom>*
means match the atom 0 or more times, so this pattern matches the0x
prefix, followed by 0 or more0
s, followed by a non-zero hex, followed by some hex.为什么不尝试一些稍微不同的东西呢? 测试非零十六进制比测试零十六进制要困难得多。 因此,测试为零并手动执行“否”。
Why not try something slighly different. Testing for a non-zero hex is much harder than testing for a zero hex. So test for zero and manually do the not.
我认为这应该涵盖所有情况(如果它确实必须是正则表达式):
I think this should cover all cases (if it really has to be a regex):
固定大小的十六进制数字可以使用负向前查找来查找,如下所示:
查找以
0x
开头的组,然后查找负向前
0{8}
(如果找到则失败),否则匹配
[0-9a-fA-F]{8}
适用于 PCRE、JavaScript、Python。 不知道哪些编辑器支持负向前瞻。
Fixed size hex numbers can be looked up using negative lookahead as:
A group is looked up beginning with
0x
then negative look ahead
0{8}
(fails if found)otherwise match
[0-9a-fA-F]{8}
Works with PCRE, JavaScript, Python. Don't know which editors support negative lookahead.
当然是一个简单的字符串比较,如果它
不等于
“0x00000000”,那么你就得到了匹配。我是否过于简化了? 只有一种
FALSE
情况,对吧? 当字符串是“0x00000000”时?除非必要,否则不要使用 RegEx。
Surely a simple string compare and if it
DOES NOT EQUAL
"0x00000000" you've got your match.Am I over simplifying it? The is only one
FALSE
case, right? When the string is "0x00000000"?Don't use RegEx unless you have to.
“
0x
”,后跟零到七个“0
”,后跟非“0
”的内容'
0x
', followed by zero to seven '0
', followed by something that is not '0
'