简体   繁体   English

带*和空格的多个单词的正则表达式

[英]Regular expression for multiple words with * and space

My regular expression is of format "Exit* Order*". 我的正则表达式的格式为“退出*订单*”。 When i use in java its not working as expected. 当我在Java中使用它不能按预期工作时。

String pattern = "Exit* Order*";
String ipLine = "Exiting orders";

Match: NO 匹配:否

String pattern = "Exit Order";
String ipLine = "Exit order";

Match: Yes. 符合:是。

Java Code: Pattern patrn = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE); Java代码:Pattern patrn = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE); Matcher match = patrn.matcher(ipLine); 匹配器匹配= patrn.matcher(ipLine);

Can any one let me know what should be the pattern in such cases. 任何人都可以让我知道在这种情况下的模式是什么。

I believe you are looking for something like: 我相信您正在寻找类似的东西:

"Exit.* Order.*"

or maybe something instead of .* , eg \\S* , \\w* , [A-Za-z]* . 或代替.*东西,例如\\S*\\w*[A-Za-z]*

Your current regular expression is looking for zero or more t and r on the ends of the words, eg it would match 您当前的正则表达式在单词的末尾寻找零个或多个tr ,例如,它将匹配

Exi Orde
Exit Orde
Exitt Orde
Exi Order
Exi Orderr
...
Exit\\w* Order\\w*

You should use this. 您应该使用此。 .* can match much more than intended.use i or ignorecase flag .*可能比预期的匹配得多.* iignorecase标志

It seems like you just want to match "Exit Order" case-insensitively: 似乎您只想不区分大小写地匹配“退出订单”:

Try this: 尝试这个:

if (str.matches("(?i)exit order"))

Or to restrict the match to just your examples, where the "O" of "Order may be "o", use: 或将匹配限制为您的示例,其中“订单”的“ O”可能为“ o”,请使用:

if (str.matches("Exit [Oo]rder"))

Java does not use Linux regexp expression: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html Java不使用Linux正则表达式: http : //docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Use this: 用这个:

String pattern = "Exit.* Order.*";

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

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