简体   繁体   English

Java正则表达式模式匹配正向

[英]Java Regular expression pattern match positive lookahead

I've create a regular expression to match 我创建了一个正则表达式来匹配

  • user_ent_mov user_ent_mov
  • user_ent_mov_123 user_ent_mov_123
  • user_mov_ent123 user_mov_ent123

But it shouldn't match 但是不应该匹配

  • user_ent user_ent
  • user_ent123 user_ent123
  • user_mov123 user_mov123

My Regual Expression 我的正常表达

^user_(?:ent(?=_mov)|mov(?=_ent)).*$

Everything is working fine. 一切正常。 But when I run exhaustive tests my regx fails. 但是,当我运行详尽的测试时,我的regx失败了。

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

PS: I've pre-compiled my regex and its not declared static. PS:我已经预编译了我的正则表达式,并且未将其声明为静态。

Keep it simple. 把事情简单化。

user_(ent_mov|mov_ent)_?\d*

DEMO 演示

use String#matches() to check for matches. 使用String#matches()检查匹配项。

String regex="user_(ent_mov|mov_ent)_?\\d*";

System.out.println("user_ent_mov".matches(regex));    //true
System.out.println("user_ent_mov_123".matches(regex));//true
System.out.println("user_mov_ent123".matches(regex)); //true
System.out.println("user_ent".matches(regex));        //false
System.out.println("user_ent123".matches(regex));     //false
System.out.println("user_mov123".matches(regex));     //false

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

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