正向匹配后的正则表达式匹配模式 [英] Regex match pattern after positive look ahead

查看:121
本文介绍了正向匹配后的正则表达式匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正向表达式,正向前进.正向超前依赖于字符串中存在"cfu/ml",但不将其包括在结果中.

I have a regex that is doing a positive lookahead. The positive lookahead relies on "cfu/ml" being present in the string but doesn not include it in the result.

这是正则表达式:

((((less|greater)\s*tha[nt]\s*)?[><]*[\d]+[\sx,.-]*)*)+(?=CFU\s?/\s?ML)

前字符串:"100,000,000 x 85 x 9345 cfu/ml"

Ex string: "100,000,000 x 85 x 9345 cfu/ml"

匹配1:"100,000,000 x 85 x 9345"

Match1: "100,000,000 x 85 x 9345"

这很好,但是尝试在正向搜索前匹配任何内容都行不通.我想做的是像这样正面看待之后再添加一个结果捕获组.

That's working just fine, but trying to match anything after that positive lookahead is not working. What I'm trying to do is add another result capture group after the positive look ahead like so.

((((less|greater)\s*tha[nt]\s*)?[><]*[\d]+[\sx,.-]*)*)+(?=CFU\s?/\s?ML)\s*blah

前字符串:"100,000,000 x 85 x 9345 cfu/ml等"

Ex string: "100,000,000 x 85 x 9345 cfu/ml blah"

匹配1:"100,000,000 x 85 x 9345"

Match1: "100,000,000 x 85 x 9345"

匹配2:等等"

Match2: "blah"

在积极的前瞻性工作之后,似乎什么都没有,有人知道我可以解决这个问题吗?

Seems like nothing after the positive look ahead works, anyone know how I can fix this?

推荐答案

冒号是零宽度 -它们不匹配任何字符,只是断言某些条件在那时是正确的在字符串中.因此,如果先行匹配,那么后面的字符将是CFU / ML或您要先行匹配的其他任何字符.

Lookaheads are zero-width -- they don't match any characters, they just assert that certain conditions are true at that point in the string. So if the lookahead matches, then the characters after it will be CFU / ML or whatever else your lookahead would match.

不过,您想忽略这些字符-这意味着不仅要断言它们存在,而且还要实际食用它们,以使它们不成为匹配组的一部分.例如,您可以使前瞻性成为非捕获组.完全匹配的字符串中仍将包含那些额外的字符,但是捕获组将不包括它们.

You want to ignore those characters, though -- which means not just asserting they exist, but actually consuming them so they don't become part of a match group. For example, you might make your lookahead be a non-capturing group instead. The full matched string will still have those extra chars in it, but the capture groups won't include them.

这篇关于正向匹配后的正则表达式匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆