Following String causes PatternSyntaxException:
Pattern.compile("*\\.*");
I want to create a pattern so that I can filter all files with the name in the following form: "*.*"
How can I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To match all strings with a
.in the name, you do:To break it down:
.*match any number of arbitrary character[.]match a dot. (yes,\\.works too).*match any number of arbitrary characterDemo:
Note that the string with just one dot,
"."matches as well. To ensure that you have some characters in front and after the dot, you could change the*to+:.+[.].+.The reason you get
PatternSyntaxException:The
*operator is to be interpreted as “the previous character repeated zero or more times”. Since you started your expression with*there was no character to repeat, thus an exception was thrown.