简体   繁体   English

如果后面跟着另一个组,如何不捕获正则表达式中的组

[英]How to not capture a group in regex if it is followed by an another group

If I have a string eg.: 'hcto,231' or 'hcto.12' I want to be able to capture 'o,231' or 'o.12' and process it as a number ('hct' is random and any other string can replace it).如果我有一个字符串,例如:'hcto,231' 或 'hcto.12' 我希望能够捕获 'o,231' 或 'o.12' 并将其作为数字处理('hct' 是随机的并且任何其他字符串都可以替换它)。 But I don't want to capture if the 'o' character if followed by a decimal number eg: 'wordo.23.12' or 'wordo,23,12'.但是我不想捕捉'o'字符是否后跟十进制数字,例如:'wordo.23.12'或'wordo,23,12'。

I've tried using the following regex:我尝试使用以下正则表达式:

([oO][.,][0-9]+)(?.([,,][0-9]+))

but it always matches.但它总是匹配。 In the string 'hct o.2 2.23' it matches the bold part, but I don't want it to match anything.在字符串 'hct o.2 2.23' 中,它匹配粗体部分,但我不希望它匹配任何内容。 Is there a way to combine groups so it won't match if the negative lookahead is true.有没有办法组合组,所以如果负前瞻为真,它将不匹配。

The match occurs in hcto.22.23 because the lookahead triggers backtracking, and since [0-9]+ match match a single 2 (it does not have to match 22 ) the match succeeds and returns a smaller, unexpected match :匹配发生在hcto.22.23因为前瞻触发回溯,并且由于[0-9]+匹配匹配单个2 (它不必匹配22 )匹配成功并返回一个较小的意外匹配

在此处输入图像描述

It seems the simplest way to fix the current issue is to make the dot or comma pattern in the lookahead optional, and remove unnecessary groups:似乎解决当前问题的最简单方法是使前瞻中的点或逗号模式可选,并删除不必要的组:

[oO][.,]\d+(?![.,]?\d)

See the regex demo .请参阅正则表达式演示

Details细节

  • [oO] - o or O [oO] - oO
  • [.,] - a dot or comma [.,] - 点或逗号
  • \d+ - one or more digits \d+ - 一位或多位数字
  • (?.[,?]?\d) - not followed with . (?.[,?]?\d) - 不跟. / , and a digit, or just with a digit. / ,一个数字,或者只是一个数字。

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

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