[英]How to use .matches for string containing .* in between characters in java
String str1 = "My hobby is playing \"Tennis\" I am good at it. I love it."
String str2 = "My hobby is playing \"Tennis\". I (Alex) am good at it. I(Alex) love it. "
I want to check if str1 matches with str2 other than Alex part. 我想检查str1是否与除Alex部分之外的str2匹配。
Note - Alex is a changing part here. 注意 - 亚历克斯是一个变化的部分。 It may be Tom, Hary etc. 可能是汤姆,哈里等。
So I tried :- 所以我试过: -
str2 = "My hobby is playing \"Tennis\". I (.*) am good at it. I(.*) love it. "
str1.matches(str2)
But it returns false. 但它返回false。
What am I doing wrong here? 我在这做错了什么?
It works with :- 它适用于: -
str2 = "My hobby is playing \"Tennis\". I (.*)
Your regex will match two white spaces between "I" and "am", so it wont match the string 你的正则表达式将匹配“I”和“am”之间的两个空格,因此它不匹配字符串
"My hobby is playing \"Tennis\" I am good at it. I love it."
Becasue there's only one whitespace there. 因为那里只有一个空白。 Moreover, you missed the dot after "Tennis". 而且,你在“网球”之后错过了点。 However, pay attention that "." 但是,要注意“。” has a special meaning in regex. 在正则表达式中有特殊含义。 So you want to escape dots in your regex 所以你想在你的正则表达式中逃脱点
Try: 尝试:
"My hobby is playing \"Tennis\"\\. I (?:\\(.*\\) )?am good at it\\. I (?:\\(.*\\) )?love it\\."
The above will match both: 以上将匹配两者:
My hobby is playing "Tennis". I am good at it. I love it.
My hobby is playing "Tennis". I (Alex) am good at it. I (Alex) love it.
You need a regex that can match the varying part. 你需要一个可以匹配不同部分的正则表达式。 The varying part here, is the name with the parentheses. 这里的变化部分是带括号的名称。 That part may or may not be there in the string. 该部分可能在字符串中,也可能不存在。 The name, can be any string of English alphabets. 名称,可以是任何英文字母字符串。 So you want to match [open-bracket][any string of alphabets][close bracket]
. 所以你想匹配[open-bracket][any string of alphabets][close bracket]
。 Let's build a regex for this. 让我们为此构建一个正则表达式。 The regex for both sentences I am good at it
and I love it
will be same, so you just have to replicate that. 两个句子的正则表达式I am good at it
并且I love it
是相同的,所以你只需要复制它。 Now: 现在:
1. There can be 0 or 1 open bracket. 1.可以有0或1个开放式支架。 0 if you're not mentioning the name at all, 1 otherwise. 如果你根本没有提到名字,则为0,否则为1。 Regex for this is: [(]?
Similarly for close bracket, you need [)]?
正则表达式是这样的: [(]?
类似的关闭括号,你需要[)]?
. 。
2. There has to be at least 1 whitespace after I
and one before am
. 2.必须有后至少1个空格I
和前一个am
。 For that, we need \\s+
, but we need to escape the \\
, so the regex for one or more whitespace becomes \\\\s+
. 为此,我们需要\\s+
,但我们需要转义\\
,因此一个或多个空格的正则表达式成为\\\\s+
。 Similarly, one space after I
and one before love
. 同样,在I
和love
之前的一个空间。
3. Then you need a regex to match names/words of English. 3.然后,你需要一个正则表达式匹配的英语名称/词。 That simply is [a-zA-Z]*
. 那只是[a-zA-Z]*
。
4. Finally, all of the above may or may not be there in the sentence, so we enclose the regex we build above, like this [regex-built-above]*
. 4.最后,上述所有内容可能也可能不在句子中,因此我们将上面构建的正则表达式包含在内,就像这个[regex-built-above]*
。 This ensures that the name with parentheses can be absent as well. 这确保了带括号的名称也可以不存在。
5. You need to escape .
5.您需要逃脱.
and \\
. 和\\
。
So finally, str2
should be: 最后, str2
应该是:
String str2 = "My hobby is playing \\"Tennis\\"\\\\. I[\\\\s+[(]?[a-zA-Z]*[)]?\\\\s+]* am good at it\\\\. I[\\\\s+[(]?[a-zA-Z]*[)]?\\\\s+]* love it\\\\."
It would match any of the following str1
: 它将匹配以下任何str1
:
str1 = "My hobby is playing \\"Tennis\\". I (Alex) am good at it. I (Alex) love it."
str1 = "My hobby is playing \\"Tennis\\". I am good at it. I love it."
Let me know if it helps. 如果有帮助,请告诉我。 It worked for me.. 它对我有用..
Helpful resources: 有用的资源:
1. http://www.tutorialspoint.com/java/java_regular_expressions.htm 1. http://www.tutorialspoint.com/java/java_regular_expressions.htm
2. http://www.tutorialspoint.com/java/java_string_matches.htm 2. http://www.tutorialspoint.com/java/java_string_matches.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.