加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby正则表达式非捕获组

发布时间:2020-12-17 04:02:42 所属栏目:百科 来源:网络整理
导读:我试图从字符串中获取id号码 id/number/2000GXZ2/ref=sr 运用 (?:id/number/)([a-zA-Z0-9]{8}) 出于某种原因,非捕获组没有工作,给我: id/number/2000GXZ2 解决方法 正如其他人所提到的,非捕获组仍然计入整体匹配.如果你不希望你的比赛中的那个部分使用loo
我试图从字符串中获取id号码

id/number/2000GXZ2/ref=sr

运用

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

出于某种原因,非捕获组没有工作,给我:

id/number/2000GXZ2

解决方法

正如其他人所提到的,非捕获组仍然计入整体匹配.如果你不希望你的比赛中的那个部分使用lookbehind.
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号的捕获组.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读