正向匹配后的正则表达式匹配模式

编程入门 行业动态 更新时间:2024-10-27 20:36:06
本文介绍了正向匹配后的正则表达式匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个正向表达式,正向前进.正向超前依赖于字符串中存在"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.

更多推荐

正向匹配后的正则表达式匹配模式

本文发布于:2023-11-29 17:46:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!