This is a method of performing complete and partial matches using the matches and contains methods of the String object.
It also describes how to use the startWith method for prefix matching and the endWith method for suffix matching.
Exact match
Explanation
Exact match only determines that the strings are the same.
Therefore, it judges without using regular expressions.
Example
1 2 3 4 5 6 7 8 |
// Exact match String str1 = "1234567890"; // True System.out.println(str1.matches("1234567890")); // Exact string match // False System.out.println(str1.matches("123456789")); // String does not match exactly |
Partial match
Prefix match
Explanation
For prefix match, use “^ (hat mark)”.
*It is called a hat because it is shaped like a hat.
“^” is the symbol that indicates the beginning of the string.
Specifies what string the beginning should match, such as “^123.*”.
If the prefix match part is a fixed value, you can use the startWith method.
In that case, please note that match judgment by regular expression is not possible.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// prefix match String str2 = "1234567890"; // True System.out.println(str2.matches("^123.*")); // prefix match of 3 characters System.out.println(str2.matches("^1234567890.*")); // 10 characters + prefix match of any 0 or more characters System.out.println(str2.matches("^123456789.")); // 9 characters + prefix match of any 1 character System.out.println(str2.startsWith("123")); // Prefix match by startWith method // False System.out.println(str2.matches("^124.*")); // 3-character prefix mismatch System.out.println(str2.matches("^1234567890.")); // 10 characters + any 1 character prefix mismatch System.out.println(str2.startsWith("234")); // Prefix mismatch in startWith method System.out.println(str2.startsWith("123.*")); // Regular expressions not allowed in startWith method |
<Supplement>
“. (dot)”
A single character of your choice.
Any character does not matter, but it indicates that there must be one character.
“* (asterisk)”
0 or more repetitions of the preceding character.
In other words, “.*” means “.” is repeated 0 or more times.
Since it can be 0 times, it also includes non-repeating patterns.
(example)
Regular expression: ^123.*
Patterns that become True: 123, 1234, 123AAAA
Suffix match
Explanation
For backward matching, use “$ (dollar)”.
“$” is the symbol that indicates the end of the string.
Specifies what string the end should match, such as “.*890$”.
If the suffix is a fixed value, you can use the endWith method.
In that case, please note that match judgment by regular expression is not possible.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// suffix match String str3 = "1234567890"; // True System.out.println(str3.matches(".*890$")); // Any 0 or more characters + 3 character suffix match System.out.println(str3.matches(".*1234567890$")); // Any 0 or more characters + 10 suffixes match System.out.println(str3.matches(".234567890$")); // Any 1 character + 9 character suffix match System.out.println(str3.endsWith("890")); // Suffix match in endsWith method // False System.out.println(str3.matches(".*190$")); // Any 0 or more characters + 3 characters backward mismatch System.out.println(str3.matches(".1234567890$")); // Any 1 character + 10 characters backward mismatch System.out.println(str3.endsWith("789")); // Backward mismatch in endsWith method System.out.println(str3.endsWith(".*890")); // Regex not allowed in endsWith method |
Partial match
Explanation
Use a combination of ‘^’ and ‘$’ for partial matches.
Like “^.*456.*$”.
Surround it with ‘^’ and ‘$’ and allow any character with ‘.*’ before and after the character.
Besides regular expressions, there is also a method using the contains method.
(example)
str4.contains(“444”);
Returns True if the string contains “444”.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// partial match String str4 = "1234567890"; // True System.out.println(str4.matches(".*456.*$")); // Partial match of 3 characters System.out.println(str4.matches("^.*1234567890.*$")); // 0 or more arbitrary characters + 10 characters + partial match of 0 or more arbitrary characters System.out.println(str4.matches("^.23456789.$")); // Partial match of any 1 character + 8 characters + any 1 character // True (contains) System.out.println(str4.contains("456")); // Partial match of any 3 characters // False System.out.println(str4.matches(".*444.*$")); // Not a partial match of 3 characters System.out.println(str4.matches("^.1234567890.$")); // Not a partial match of any 1 character + 10 characters + any 1 character // False (contains) System.out.println(str4.contains("444")); // Not a partial match of any three characters |
Conclusion
・Exact match means that the character strings are exactly the same.
・Partial matching includes forward/backward/partial matching.
・Use ^ for prefix match.
・Use $ for suffix matching.
・Regular expressions using ^ and $ or the contains method can be used for partial matching.
・You can specify arbitrary characters with . and * to make flexible judgments.
Refer to
[Java Docs] String matches(String regex)
Comments