Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such a thing as a wildcard character in Java?

I'm running a comparison program and at the minute it does a direct 'string-to-string' comparison and if they are an exact match it outputs that they are a match.

Well, I was hoping to add an additional feature that allowed for 'similarity'...

so for example:

String em1 = "52494646";
String em2 = "52400646";


if (em1.equals(em2)){
    output.writeUTF(dir + filenames[i]);
}

This is sort of a snippet of the code. I'd like it so that it skips over the "00" and still recognises it as 'almost' the same number and still outputs it.

I'd imagine it would look something like String em2 = "524"+ ## +"646" but thats obviously just a concept

Does anyone know if there is a way to have this kind of 'wildcard' (a term I've picked up from uni SQL) or if there is another way to do this similarity type deal.

Thanks :)

like image 611
user585522 Avatar asked Feb 10 '11 14:02

user585522


1 Answers

You can use regular expressions:

if (em1.matches("524[0-9]{2}646")) {
  // do stuff
}

For Java specific documentation see the Pattern class. For some uses of regular expressions (such as in the sample above), there are shortcut methods in String: matches(), replaceAll()/replaceFirst() and split().

regular-expressions.info has good documentation on regular expression in general.

like image 172
Joachim Sauer Avatar answered Nov 25 '22 13:11

Joachim Sauer