PatternSyntaxException

After the line, it calls PatternSyntaxException:

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

I want to create a template so that I can filter all files with a name in the following form: "*.*"

How can i do this?

+5
source share
6 answers

To match all the strings with .in the name, follow these steps:

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

To break it:

  • .* matches any number of arbitrary characters
  • [.]corresponds to a point. (yes, \\.works too)
  • .* matches any number of arbitrary characters

Demo:

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

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

Note that a single dot line "."also matches. To make sure that you have some characters in front and after a point, you can change *to +: .+[.].+.


The reason you get PatternSyntaxException:

* " , ". *, , .

+3

* , . Java Pattern : http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

, :

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

, :

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

, " ",

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

:

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

* ( ) . , \\.

:

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

* , [ ].

*.*, :

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

( ),

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

, "." , :

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

Pattern.compile(".*\\/.*\\..*"); . , . - . , , *, 0 . .
, C:/folder.1/folder.2/file.txt :
.* - C:/folder.1/folder.2 ( , /)
\/ - /
.* - file (, . ())
\\. - . ()
.* - txt ( . ( ))

0

All Articles