gpt4 book ai didi

python - 后视模式无效

转载 作者:数据小太阳 更新时间:2023-10-29 07:54:43 25 4
gpt4 key购买 nike

为什么这个正则表达式在 Python 中有效但在 Ruby 中无效:

/(?<!([0-1\b][0-9]|[2][0-3]))/

很高兴听到解释以及如何在 Ruby 中绕过它

编辑整行代码:

re.sub(r'(?<!([0-1\b][0-9]|[2][0-3])):(?!([0-5][0-9])((?i)(am)|(pm)|(a\.m)|(p\.m)|(a\.m\.)|(p\.m\.))?\b)' , ':\n' , s)

基本上,当有冒号而不是时间时,我会尝试添加 '\n'

最佳答案

Ruby 正则表达式引擎不允许在 look behinds 中捕获组。如果需要分组,可以使用非捕获组 (?:):

[8] pry(main)> /(?<!(:?[0-1\b][0-9]|[2][0-3]))/
SyntaxError: (eval):2: invalid pattern in look-behind: /(?<!(:?[0-1\b][0-9]|[2][0-3]))/
[8] pry(main)> /(?<!(?:[0-1\b][0-9]|[2][0-3]))/
=> /(?<!(?:[0-1\b][0-9]|[2][0-3]))/

Docs:

 (?<!subexp)        negative look-behind

Subexp of look-behind must be fixed-width.
But top-level alternatives can be of various lengths.
ex. (?<=a|bc) is OK. (?<=aaa(?:b|cd)) is not allowed.

In negative look-behind, capturing group isn't allowed,
but non-capturing group (?:) is allowed.

学习自this answer .

关于python - 后视模式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57102938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com