gpt4 book ai didi

ruby - 正则表达式多次匹配 OR 向后看

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:17 25 4
gpt4 key购买 nike

我有以下字符串:

'/photos/full/1/454/6454.jpg?20140521103415','/photos/full/2/452/54_2.jpg?20140521104743','/photos/full/3/254/C2454_3.jpg?20140521104744'

我要解析的是来自/的地址到 ?但我似乎无法弄清楚。

到目前为止我有/(?<=')[^?]*/这将正确获得第一个链接,但第二个和第三个链接将以 ,'/photos/full/... 开头<--注意它以 ,' 开头

如果我然后尝试 /(?<=',')[^?]*/我得到了第二个和第三个链接,但错过了第一个链接。

除了做 2 个正则表达式,有没有一种方法可以将它们组合起来做 1 个?我试过使用 `/((?<=')|(?<=',')[^?]*/无济于事。

我的代码的形式是 matches = string.scan(regex)然后我运行 match.each阻止...

最佳答案

在具有 \K 的 Ruby 2 中,您可以使用这个简单的正则表达式(参见 demo):

'\K/[^?]+

查看所有匹配项:

regex = /'\K\/[^?]+/
subject.scan(regex) {|result|
# inspect result
}

解释正则表达式

'                        # '\''
\K # 'Keep Out!' abandons what we have matched so far
\/ # '/'
[^?]+ # any character except: '?' (1 or more times
# (matching the most amount possible))

关于ruby - 正则表达式多次匹配 OR 向后看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255040/

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