简体   繁体   English

为什么我的Python正则表达式找到两个匹配项?

[英]Why does my Python regular expression find two matches?

I am using [0x]?\\d+ to attempt to locate strings of the nature 我正在使用[0x]?\\d+尝试查找性质为字符串的字符串

0x10101
0x12341
12321

But when I run the regular expression on 0x3 , I get [u'0', u'x3'] . 但是当我在0x3上运行正则表达式时,我得到[u'0', u'x3']

It seems (to my untrained eye) that it is matching the shortest sequences? (以我未经训练的眼睛)似乎与最短序列匹配? Or is there something wrong with my use of []? 还是我对[]?使用有问题[]? ?

[...] , which is called a character class , matches a single character. [...]称为字符类 ,与单个字符匹配。

You probably want: 您可能想要:

(?:0x)?\d+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM