简体   繁体   中英

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.

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.

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

"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] . 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. Now:

1. There can be 0 or 1 open bracket. 0 if you're not mentioning the name at all, 1 otherwise. 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 . For that, we need \\s+ , but we need to escape the \\ , so the regex for one or more whitespace becomes \\\\s+ . Similarly, one space after I and one before love .

3. Then you need a regex to match names/words of English. That simply is [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]* . This ensures that the name with parentheses can be absent as well.

5. You need to escape . and \\ .

So finally, str2 should be:

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 = "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

2. http://www.tutorialspoint.com/java/java_string_matches.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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