gpt4 book ai didi

Ruby 正则表达式非捕获组

转载 作者:数据小太阳 更新时间:2023-10-29 06:57:48 24 4
gpt4 key购买 nike

我正在尝试从字符串中获取 ID 号,比如说

id/number/2000GXZ2/ref=sr

使用

(?:id\/number\/)([a-zA-Z0-9]{8})

由于某种原因,非捕获组不起作用,给我:

id/number/2000GXZ2

最佳答案

正如其他人所提到的,非捕获组仍然计入总比赛。如果您不想在比赛中使用该部分,请使用后视。 Rubular example

(?<=id\/number\/)([a-zA-Z0-9]{8})

(?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text

Ruby Doc Regexp

另外,在这种情况下,id 号周围的捕获组是不必要的。

关于Ruby 正则表达式非捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462038/

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