Using Regular Expressions
String a = "I love Java";
assert a.matches("I love Java"); // match whole string
assert a.matches(".*love.*"); // contains 'love'? (more)
// Escaping a search string (more)
String anyString = ...; // works with any string
boolean containsString = a.matches(".*" + Matcher.quoteReplacement(anyString) + ".*");
// Find all numbers in a string:
String a = "6 times 7 is 42";
Pattern pattern = Pattern.compile("[0-9]+"); // compile pattern (more)
Matcher m = pattern.matcher(a); // create Matcher (more)
while (m.find()) { // loop through all matches (more)
System.out.println(m.group() + // print the match/number
" at "+ m.start() + " - " + m.end()) // print position (more)
}
// Parse an email address:
String b = "tom@example.org";
Pattern emailPtrn = Pattern.compile("([^@]+)@([^@]+)"); // Pattern with capturing groups (more)
Matcher em = emailPtrn.matcher(b)
if (em.matches(b)); // match whole string (more)
System.out.println("Name=" + em.group(1) + " Host=" + em.group(2));
String a = "r2d2";
assert a.replaceAll("[a-z]", "00").equals("002002");
assert a.replaceFirst("[a-z]", "00").equals("002d2");
Pattern p = Pattern.compile("[a-z]"); // with pre-compiled Pattern (more)
Matcher m = p.matcher(a);
assert m.replaceAll("00").equals("002002");
assert m.replaceFirst("00").equals("002d2");
// Replace all dates in format yyyy-mm-dd with ddmmyyyy in the string:
String a = "2009/12/24, 2009/12/31, 2010/01/01";
Pattern p = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher m = p.matcher(a);
StringBuffer result = new StringBuffer();
while (m.find()) // loop through all results
m.appendReplacement(result, m.group(3)+m.group(2)+m.group(3)); // add replacement (more)
m.appendTail(result); // important! (more)
System.out.println(result.toString());
String a = "42, 17, 24, 5, 175";
String[] r = a.split("[ ,]+"); // comma and space split (more)
assert r.length == 5; // split into 5 substrings
assert r[0].equals("42") && r[1].equals("17"); // ...
String b = "-3--2";
String[] r2 = b.split("-");
assert r2.length == 4;
assert r2[0].equals("") && r2[2].equals(""); // substrings may be empty
String[] r3 = a.split("[ ,]+", 3); // limit to 3 (more)
assert r3.length == 3; // limited array size
assert r3[2].equals("24, 5, 175"); // contains remaining text
String c = "1, 2,, 3,,,";
String[] r4 = a.split("[ ,]+", 0); // remove empty strings at the end (more)
assert r4.length == 4; // empty strings at the end removed
assert r4[2].equals(""); // other empty strings remain
// Pattern has exact equivalents of String's split methods:
Pattern p = Pattern.compile(("[ ,]+");
assert p.split(a).length == 5;
assert p.split(a, 3).length == 3; // with limit
// Case-insensitive matching:
String a = "I lOvE jAvA";
Pattern p = Pattern.compile(".*love.*", Pattern.CASE_INSENSITIVE); // ASCII only! (more)
assert p.matcher(a).matches();
// Find all lines containing numbers:
String b = "23\naSd\n5\nLOVE\n\NYE\n3\n";
Pattern p2 = Pattern.compile("^\\d+$", Pattern.MULTILINE); // multiline (more)
Matcher m2 = p2.matcher(b);
while (m2.find())
System.out.println(m2.group()); // print numbers 23, 5 and 3
// Patterns that may span several lines:
String c = "a\n\b\nc";
Pattern p3 = Pattern.compile("a.*b.*c", Pattern.DOTALL); // "." matches newlines (more)
assert p3.matcher(c).matches();
assert !c.matches("a.*b.*c"); // no match without DOTALL
// Multiple flags:
Pattern p4 = Pattern.compile(".*love.*",
Pattern.CASE_INSENSITIVE | DOTALL); // combining flags (more)
assert p4.matcher(b).matches();

