5

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?

6 Answers 6

3

To match all strings with a . in the name, you do:

Pattern.compile(".*[.].*");

To break it down:

  • .* match any number of arbitrary character
  • [.] match a dot. (yes, \\. works too)
  • .* match any number of arbitrary character

Demo:

Pattern p = Pattern.compile(".*[.].*");

System.out.println(p.matcher("hello.txt").matches()); // true
System.out.println(p.matcher("hellotxt").matches());  // false

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.

Sign up to request clarification or add additional context in comments.

Comments

1

The * character has a different meaning in regular expressions than when used on the command line as a file wildcard. More information about the Java Pattern regular expression syntax can be found here: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

This will find the text you are looking to match:

Pattern.compile(".*\\..*");

Comments

0

Maybe you meant:

Pattern.compile("\\*\\.\\*");

If not, and asterisk means 'any character' then

Pattern.compile(".*\\..*");

Comments

0

If you mean, you wand to filter out files with a dot "." in its name, use this one:

Pattern.compile("[^\\.]*\\.[^\\.]*")

1 Comment

No need to escape the . in a character class.
0

You have to write:

Pattern.compile("\\*\\.\\*");

Because * has a special meaning (zero or more) in a regex. So you have to escape it with a \\.

Another way could be:

Pattern.compile("[*][.][*]");

because * loses its other significance when it appears between [ and ].

If you want to parse a filename of format *.*, this should be enough:

Pattern.compile(".+\\..+");

To match any filename (which may or may not have an extension) you can use

Pattern.compile(".+(\\..+)?");

Comments

0

Pattern.compile(".*\\/.*\\..*"); use this pattern.It will match all file names which are containing a dot. Asterisk is a special character. It means that the symbol coming before * can be 0 or more times. So you can't write it without character before.
For example for a file name C:/folder.1/folder.2/file.txt pattern will work so:
.* - C:/folder.1/folder.2 (everything until finds the last / character)
\/ - / character
.* - file (everything until finds the last . character (dot))
\\. - . (dot)
.* - txt (everything after . (actually the file extention))

3 Comments

invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
I just missed a \ for escaping / and edited my answer just now.
why would you want to escape / anyway?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.