如何查找 Java 字符串是否包含 X 或 Y 并包含 Z
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1437865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find if a Java String contains X or Y and contains Z
提问by Steve McLeod
I'm pretty sure regular expressions are the way to go, but my head hurts whenever I try to work out the specific regular expression.
我很确定正则表达式是要走的路,但是每当我尝试计算特定的正则表达式时,我的头都会受伤。
What regular expression do I need to find if a Java String (contains the text "ERROR" or the text "WARNING") AND (contains the text "parsing"), where all matches are case-insensitive?
如果 Java 字符串(包含文本“错误”或文本“警告”)和(包含文本“解析”),其中所有匹配项都不区分大小写,我需要什么正则表达式?
EDIT: I've presented a specific case, but my problem is more general. There may be other clauses, but they all involve matching a specific word, ignoring case. There may be 1, 2, 3 or more clauses.
编辑:我已经提出了一个具体案例,但我的问题更普遍。可能还有其他子句,但它们都涉及匹配特定单词,忽略大小写。可能有 1、2、3 或更多子句。
采纳答案by RichieHindle
If you're not 100% comfortable with regular expressions, don't try to use them for something like this. Just do this instead:
如果您对正则表达式不是 100% 满意,请不要尝试将它们用于此类用途。只需这样做:
string s = test_string.toLowerCase();
if (s.contains("parsing") && (s.contains("error") || s.contains("warning")) {
....
because when you come back to your code in six months time you'll understand it at a glance.
因为当你在六个月后回到你的代码时,你会一目了然。
Edit:Here's a regular expression to do it:
编辑:这是一个正则表达式来做到这一点:
(?i)(?=.*parsing)(.*(error|warning).*)
but it's rather inefficient. For cases where you have an OR condition, a hybrid approach where you search for several simple regular expressions and combine the results programmatically with Java is usually best, both in terms of readability and efficiency.
但它相当低效。对于具有 OR 条件的情况,在可读性和效率方面,搜索几个简单的正则表达式并将结果以编程方式与 Java 结合的混合方法通常是最好的。
回答by Khodor
try:
尝试:
If((str.indexOf("WARNING") > -1 || str.indexOf("ERROR") > -1) && str.indexOf("parsin") > -1)
回答by Kshitij Saxena -KJ-
Regular Expressions are not needed here. Try this:
这里不需要正则表达式。尝试这个:
if((string1.toUpperCase().indexOf("ERROR",0) >= 0 ||
string1.toUpperCase().indexOf("WARNING",0) >= 0 ) &&
string1.toUpperCase().indexOf("PARSING",0) >= 0 )
This also takes care of the case-insensitive criteria
这也照顾到不区分大小写的标准
回答by Jo?o Silva
If you really want to use regular expressions, you can use the positive lookaheadoperator:
如果你真的想使用正则表达式,你可以使用正向前瞻运算符:
(?i)(?=.*?(?:ERROR|WARNING))(?=.*?parsing).*
Examples:
例子:
Pattern p = Pattern.compile("(?=.*?(?:ERROR|WARNING))(?=.*?parsing).*", Pattern.CASE_INSENSITIVE); // you can also use (?i) at the beginning
System.out.println(p.matcher("WARNING at line X doing parsing of Y").matches()); // true
System.out.println(p.matcher("An error at line X doing parsing of Y").matches()); // true
System.out.println(p.matcher("ERROR Hello parsing world").matches()); // true
System.out.println(p.matcher("A problem at line X doing parsing of Y").matches()); // false
回答by Andrey Adamovich
I usually use this appletto experiment with reg. ex. The expression may look like this:
我通常使用这个小程序来试验 reg。前任。表达式可能如下所示:
if (str.matches("(?i)^.*?(WARNING|ERROR).*?parsing.*$")) {
...
But as stated in above answers it's better to not use reg. ex. here.
但如上述答案所述,最好不要使用 reg。前任。这里。
回答by Pascal Thivent
I think this regexp will do the trick (but there must be a better way to do it):
我认为这个正则表达式可以解决问题(但必须有更好的方法来做到这一点):
(.*(ERROR|WARNING).*parsing)|(.*parsing.*(ERROR|WARNING))
回答by Carlos Tasada
If you've a variable number of words that you want to match I would do something like that:
如果您想要匹配的单词数量可变,我会这样做:
String mystring = "Text I want to match";
String[] matchings = {"warning", "error", "parse", ....}
int matches = 0;
for (int i = 0; i < matchings.length(); i++) {
if (mystring.contains(matchings[i]) {
matches++;
}
}
if (matches == matchings.length) {
System.out.println("All Matches found");
} else {
System.out.println("Some word is not matching :(");
}
Note: I haven't compiled this code, so could contain typos.
注意:我尚未编译此代码,因此可能包含拼写错误。
回答by g1smd
With multiple .*
constucts the parser will invoke thousands of "back off and retry" trial matches.
对于多个.*
构造,解析器将调用数千个“退避并重试”试验匹配。
Never use .*
at the beginning or in the middle of a RegEx pattern.
切勿.*
在 RegEx 模式的开头或中间使用。