正则表达式仅提前删除最后一个字符 [英] Regex lookahead only removing the last character

查看:236
本文介绍了正则表达式仅提前删除最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将创建一个正则表达式来搜索文本,但前提是匹配项后没有破折号.我为此使用前瞻性:

Im creating a regex that searches for a text, but only if there isnt a dash after the match. Im using lookahead for this:

  • 正则表达式:Text[\s\.][0-9]*(?!-)

Expected result Result --------------- ------- Text 11 Text 11 Text 11 Text 52- <No Match> Text 5

Expected result Result --------------- ------- Text 11 Text 11 Text 11 Text 52- <No Match> Text 5

测试用例: https://regex101.com/r/doklxc/1/

前瞻似乎只与前一个字符匹配,这使我留有Text 5,而我需要它根本不返回匹配项.

The lookahead only seems to be matching with the previous character, which leaves me with Text 5, while I need it to not return a match at all.

我在检查 https://www.regular-expressions.info/指南,并尝试了使用小组,但我无法将其包裹住.

Im checking the https://www.regular-expressions.info/ guides and tried using groups, but I cant wrap my head around this one.

如何做到这一点,以使lookbehind函数影响整个前面的比赛?

How can I make it so the lookbehind function affects the entire preceding match?

我使用默认的.Net Text.RegularExpressions库.

Im using the default .Net Text.RegularExpressions library.

推荐答案

即使存在-[0-9]*也会回溯并让正则表达式引擎找到匹配项.

The [0-9]* backtracks and lets the regex engine find a match even if there is a -.

有两种方法:要么使用原子组,要么在前瞻中检查数字:

There are two ways: either use atomic groups or check for a digit in the lookahead:

Text[\s.][0-9]*(?![-\d])

Text(?>[\s.][0-9]*)(?!-)

请参见 regex演示#1 详细信息

  • Text[\s.][0-9]*(?![-\d])匹配Text,然后是一个点或一个空格,然后是0个或多个数字,然后它立即检查右边是否有-或数字,如果存在,则失败.比赛.即使试图回溯并匹配比以前抓取的位数更少的数字,超前的\d也会使这些尝试失败
  • Text(?>[\s.][0-9]*)(?!-)匹配Text,然后一个原子组开始,在该模式下,当组模式找到其匹配的文本后,将不允许回溯. (?!-)仅在[0-9]*模式尝试获取任何数字后才检查-.
  • Text[\s.][0-9]*(?![-\d]) matches Text, then a dot or a whitespace, then 0 or more digits, and then it checks of there is a - or digit immediately to the right, and if there is, it fails the match. Even when trying to backtrack and match fewer digits than it grabbed before, the \d in the lookahead will fail those attempts
  • Text(?>[\s.][0-9]*)(?!-) matches Text, then an atomic group starts where backtracking won't be let in after the group patterns find their matching text. (?!-) only checks for a - after the [0-9]* pattern tries to grab any digits.

这篇关于正则表达式仅提前删除最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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