简体   繁体   English

多次匹配非捕获组

[英]Match non-capturing group multiple times

I tried really hard to make a good title, but I'm not sure if I'm asking this right.我非常努力地制作一个好的标题,但我不确定我是否问对了。 Here's my best attempt:这是我最好的尝试:

I'm using Python's flavor of regex我正在使用 Python 的正则表达式

I need to match numbers using named groups:我需要使用命名组匹配数字:

15x20x30    ->  'values': [15,20,30]
15bits      ->  'values': [15]
15          ->  'values': [15]
x15         ->  'values': [15]

but should not match:但不应匹配:

456.48
888,12
6,4.8,4684.,6

my best attempt so far has been:到目前为止,我最好的尝试是:

((?:[\sa-z])(?P<values>\d+)(?:[\sa-z]))

I'm using [\sa-z] instead of a word-boundary because 15x20 are two different values.我使用[\sa-z]而不是单词边界,因为15x20是两个不同的值。

But it fails to match both 15 and 20 for the 15x20 case.但是对于15x20的情况,它无法匹配1520 It does work if I put an extra space as in 15x 20 .如果我在15x 20中放置一个额外的空间,它确实有效。 How do I tell it to "reset" the non-capturing group at the end so it also works for the non-capturing group at the beginning?我如何告诉它在最后“重置”非捕获组,以便它在开始时也适用于非捕获组?

You may use您可以使用

(?<![^\sa-z])\d+(?![^\sa-z])

Case insensitive version:不区分大小写的版本:

(?i)(?<![^\sa-z])\d+(?![^\sa-z])

Or, compile the pattern with the re.I / re.IGNORECASE flags.或者,使用re.I / re.IGNORECASE标志编译模式。

See the regex demo查看正则表达式演示

Details细节

  • (?<![^\sa-z]) - a negative lookbehind that fails the match if, immediately to the left, there is no whitespace or a lowercase letter (any ASCII letter if (?i) or re.I are used) (?<![^\sa-z]) - 如果紧靠左侧没有空格或小写字母(如果使用(?i)re.I ,则为任何 ASCII 字母,则匹配失败) )
  • \d+ - 1+ digits \d+ - 1+ 位
  • (?![^\sa-z]) - a negative lookahead that fails the match if, immediately to the right, there is no whitespace or a lowercase letter (any ASCII letter if (?i) or re.I are used) (?![^\sa-z]) - 如果紧邻右侧没有空格或小写字母(如果使用(?i)re.I任何 ASCII 字母),则匹配失败的负前瞻

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

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