简体   繁体   English

正则表达式负向超前无法正常工作

[英]Regex negative lookahead not working as expected

I have the following regex: 我有以下正则表达式:

[a-zA-Z0-9. ]*(?!cs)

and the string 和字符串

Hotfix H5.12.1.00.cs02_ADV_LCR

I want to match only untill 我只想匹配直到

Hotfix H5.12.1.00

But the regex matches untill "cs02" 但是正则表达式匹配直到“ cs02”

Shouldn't the negative lookahead have done the job? 负面的前瞻不应该完成这项工作吗?

You need to use positive lookahead instead of negative lookahead. 您需要使用正向先行而不是负向先行。

[a-zA-Z0-9. ]*(?=\.cs)

or 要么

[a-zA-Z0-9. ]+(?=\.cs)

Note that your regex [a-zA-Z0-9. ]*(?!cs) 请注意,您的正则表达式[a-zA-Z0-9. ]*(?!cs) [a-zA-Z0-9. ]*(?!cs) is greedy and matches all the characters until it reaches a boundary which isn't followed by cs . [a-zA-Z0-9. ]*(?!cs)是贪婪的,匹配所有字符,直到到达不跟cs的边界为止。 See here . 这里

At first pattern [a-zA-Z0-9. ]+ 在第一个模式[a-zA-Z0-9. ]+ [a-zA-Z0-9. ]+ matches Hotfix H5.12.1.00.cs02 greedily because this pattern greedily matches alphabets , dots and spaces. [a-zA-Z0-9. ]+贪婪地匹配Hotfix H5.12.1.00.cs02因为此模式贪婪地匹配字母,点和空格。 Once it see the underscore char, it stops matching where the two conditions is satisfied, 看到下划线字符后,它将在满足两个条件的地方停止匹配,

  1. _ won't get matched by [a-zA-Z0-9. ]+ _不会与[a-zA-Z0-9. ]+ [a-zA-Z0-9. ]+
  2. _ is not cs _不是cs

It works same for the further two matches also. 其他两场比赛也一样。

You may consider using a tempered greedy token : 您可以考虑使用脾气暴躁的令牌

(?:(?!\.cs)[a-zA-Z0-9. ])*

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

This will work regardless of whether .cs is present in the string or not because the tempered greedy token matches any 0+ characters from the [a-zA-Z0-9. ] 无论字符串中是否存在.cs这都将起作用,因为调和的贪婪令牌与[a-zA-Z0-9. ]中的任何0+个字符匹配[a-zA-Z0-9. ] [a-zA-Z0-9. ] character class that is not .cs . [a-zA-Z0-9. ]字符类,而不是.cs

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

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