Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard matching in Java

I'm writing a simple debugging program that takes as input simple strings that can contain stars to indicate a wildcard match-any

*.wav  // matches <anything>.wav
(*, a) // matches (<anything>, a)

I thought I would simply take that pattern, escape any regular expression special characters in it, then replace any \\* back to .*. And then use a regular expression matcher.

But I can't find any Java function to escape a regular expression. The best match I could find is Pattern.quote, which however just puts \Q and \E at the begin and end of the string.

Is there anything in Java that allows you to simply do that wildcard matching without you having to implement the algorithm from scratch?

like image 745
Johannes Schaub - litb Avatar asked Jun 21 '14 02:06

Johannes Schaub - litb


People also ask

What is wildcard pattern matching?

A wildcard pattern is a series of characters that are matched against incoming character strings. You can use these patterns when you define pattern matching criteria. Matching is done strictly from left to right, one character or basic wildcard pattern at a time.

What is wildcard give example?

A wildcard is a symbol that takes the place of an unknown character or set of characters. Commonly used wildcards are the asterisk ( * ) and the question mark ( ? ). Depending on the software or the search engine you are using, other wildcard characters may be defined.

What are wildcards in testing?

Wildcards are special symbols that can be used to match characters in string values. TestComplete supports two standard wildcards: the asterisk (*) and the question mark (?). The asterisk wildcard corresponds to a string of any length (including an empty string). The question mark corresponds to any single character.


1 Answers

Just escape everything - no harm will come of it.

    String input = "*.wav";
    String regex = ("\\Q" + input + "\\E").replace("*", "\\E.*\\Q");
    System.out.println(regex); // \Q\E.*\Q.wav\E
    System.out.println("abcd.wav".matches(regex)); // true

Or you can use character classes:

    String input = "*.wav";
    String regex = input.replaceAll(".", "[$0]").replace("[*]", ".*");
    System.out.println(regex); // .*[.][w][a][v]
    System.out.println("abcd.wav".matches(regex)); // true

It's easier to "escape" the characters by putting them in a character class, as almost all characters lose any special meaning when in a character class. Unless you're expecting weird file names, this will work.

like image 77
Bohemian Avatar answered Sep 21 '22 00:09

Bohemian