Java Wildcard String Matching
Posted 2008-12-15 in Java by Johann.
This entry contains code examples of Java pattern matching with wildcards.
Note that wildcard matching is not the same as the .* regular expression which matches any number of characters – a wildcard matches only one character. Wildcards are usually encoded as ., but the actual value may vary across libraries.
StringSearch
StringSearch 1.2 comes with two wildcard pattern matching algorithms, BNDMWildcards and ShiftOrWildcards. Generally, BNDMWildcards will be faster, which is why I removed ShiftOrWildcards in version 2.
public void testStringSearch() {
BNDMWildcards bndm = new BNDMWildcards();
Object compiled = bndm.processString("bla.blorb");
// "bla?blorb" for StringSearch 1.2
assertEquals(3,
bndm.searchString("la bla0blorb null", "bla.blorb", compiled));
}
java.util.regex.Pattern
The java.util.regex.Pattern API isn’t very compact, but of course it does offer more than just wildcards.
public void testJavaUtilRegex() {
Pattern searchPattern = Pattern.compile("bla.blorb");
Matcher m = searchPattern.matcher("la bla0blorb null");
assertTrue(m.find());
assertEquals(3, m.start());
}
Jakarta Oro
ORO offers many PatternMatcher implementations. In this example, I am using the Perl5Matcher class.
public void testJakartaORO() throws MalformedPatternException {
Pattern p = new Perl5Compiler().compile("bla.blorb");
Perl5Matcher matcher = new Perl5Matcher();
assertTrue(matcher.contains("la bla0blorb null", p));
MatchResult result = matcher.getMatch();
assertEquals(3, result.beginOffset(0));
}
Case Insensitive Search
All of the APIs presented here support case-insensitive string matching. The case insensitive option is simply compiled into the pattern in the compile phase.
StringSearch
This example requires StringSearch version 2 or greater.
BNDMWildcardsCI bndm = new BNDMWildcardsCI();
Object compiled = bndm.processString("bla.blorb");
…
java.util.regex.Pattern
Pattern searchPattern = Pattern.compile("bla.blorb",
Pattern.CASE_INSENSITIVE);
…
Jakarta ORO
Pattern p = new Perl5Compiler().compile("bla.blorb",
Perl5Compiler.CASE_INSENSITIVE_MASK);
…
Subscribe
RSS 2.0, Atom or subscribe by Email.
Top Posts
- DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
- 6 fast jQuery Tips: More basic Snippets
- xslt.js version 3.2 released
- xslt.js version 3.0 released XML XSLT now with jQuery plugin
- Forum Scanners - prevent forum abuse
- Automate JavaScript compression with YUI Compressor and /packer/