S
S
Stepan2021-12-12 18:40:19
Java
Stepan, 2021-12-12 18:40:19

How to convert a string with special characters (mask) into a pattern?

Hello.

I am writing a method to search for a file by name. To search for matches by substring, I solved the problem like this:

...
fileNameQuery = "asd";
...
if (file.getName().matches(".*" + asd + ".*"))
...

But I don't really like this solution.

Hence the question: maybe in the bowels of Java there is a ready-made method that converts:
" *sdf*.txt"to a pattern" .*sdf.*\.txt",
" *sdf?.txt"to a pattern" .*sdf.+\.txt",
" sd*.tx?"to a pattern" sd.*\.tx?+", etc.

Or is it just manual replacements?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
roswell, 2021-12-12
@roswell

There is in the bowels, but it's not Core Java. WildcardFileFilter from commons-io .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question