RegexTestpublic class RegexTest extends TestCase Tests basic functionality of Pattern and Matcher classes. |
Fields Summary |
---|
public static final Pattern | TOP_LEVEL_DOMAIN_PATTERN | public static final Pattern | EMAIL_ADDRESS_PATTERN |
Methods Summary |
---|
public void | testFind()
Pattern p = Pattern.compile(".");
Matcher m = p.matcher("abc");
assertTrue(m.find());
assertEquals("a", m.group(0));
assertTrue(m.find());
assertEquals("b", m.group(0));
assertTrue(m.find());
assertEquals("c", m.group(0));
assertFalse(m.find());
| public void | testGroupCount()
Pattern p = Pattern.compile(
"\\b(?:\\+?1)?"
+ "(?:[ -\\.])?"
+ "\\(?(\\d{3})?\\)?"
+ "(?:[ -\\.\\/])?"
+ "(\\d{3})"
+ "(?:[ -\\.])?"
+ "(\\d{4})\\b"
);
Matcher m = p.matcher("1 (919) 555-1212");
assertEquals("groupCount is incorrect, see http://b/issue?id=759412",
3, m.groupCount());
| public void | testGroups()
Pattern p = Pattern.compile("(b)([c|d])(z*)");
Matcher m = p.matcher("abcdefg");
/* Must call find() first, otherwise group*() are undefined. */
assertTrue(m.find());
assertEquals(3, m.groupCount());
assertEquals("bc", m.group(0));
assertEquals("b", m.group(1));
assertEquals("c", m.group(2));
assertEquals("", m.group(3));
| public void | testMatches()
/* Tests class Matcher */
Pattern p = Pattern.compile("bcd");
Matcher m = p.matcher("bcd");
assertTrue("Should match.", m.matches());
/* Pattern in the middle */
p = Pattern.compile("bcd");
m = p.matcher("abcdefg");
assertFalse("Should not match.", m.matches());
/* Pattern at the head */
m = p.matcher("bcdefg");
assertFalse("Should not match.", m.matches());
/* Pattern at the tail */
m = p.matcher("abcd");
assertFalse("Should not match.", m.matches());
/* Make sure matches() doesn't change after calls to find() */
p = Pattern.compile(".*");
m = p.matcher("abc");
assertTrue(m.matches());
assertTrue(m.find());
assertTrue(m.matches());
p = Pattern.compile(".");
m = p.matcher("abc");
assertFalse(m.matches());
assertTrue(m.find());
assertFalse(m.matches());
/* Make sure matches() agrees after a reset() */
m.reset("z");
assertTrue(m.matches());
m.reset("xyz");
assertFalse(m.matches());
/* Tests class Pattern */
assertFalse("Erroneously matched partial string. " +
"See http://b/issue?id=754601", Pattern.matches("er", "xer"));
assertFalse("Erroneously matched partial string. " +
"See http://b/issue?id=754601", Pattern.matches("xe", "xer"));
assertTrue("Generic regex should match.",
Pattern.matches(".*", "bcd"));
assertTrue("Grouped regex should match.",
Pattern.matches("(b(c(d)))", "bcd"));
assertTrue("Grouped regex should match.",
Pattern.matches("(b)(c)(d)", "bcd"));
| public void | testMonsterRegexCorrectness()
assertTrue(EMAIL_ADDRESS_PATTERN.matcher("a+b@gmail.com").matches());
| public void | testMonsterRegexPerformance()
android.util.Log.e("RegexTest", "RegEx performance test started.");
long t0 = System.currentTimeMillis();
Matcher m = EMAIL_ADDRESS_PATTERN.matcher("donot repeate@RC8jjjjjjjjjjjjjjj");
assertFalse(m.find());
long t1 = System.currentTimeMillis();
android.util.Log.e("RegexTest", "RegEx performance test finished, " +
"took " + (t1 - t0) + " ms.");
| public void | testReplaceAll()
// Begins with non-matching text, ends with matching text
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("fooaabfooaabfooabfoob");
String r = m.replaceAll("-");
assertEquals("foo-foo-foo-foo-", r);
// Begins with matching text, ends with non-matching text
p = Pattern.compile("a*b");
m = p.matcher("aabfooaabfooabfoobfoo");
r = m.replaceAll("-");
assertEquals("-foo-foo-foo-foo", r);
| public void | testReplaceFirst()
// Begins with non-matching text, ends with matching text
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("fooaabfooaabfooabfoob");
String r = m.replaceFirst("-");
assertEquals("foo-fooaabfooabfoob", r);
// Begins with matching text, ends with non-matching text
p = Pattern.compile("a*b");
m = p.matcher("aabfooaabfooabfoobfoo");
r = m.replaceFirst("-");
assertEquals("-fooaabfooabfoobfoo", r);
| public void | testSplit()
Pattern p = Pattern.compile(":");
String[] strings;
strings = p.split("boo:and:foo");
assertEquals(3, strings.length);
assertEquals("boo", strings[0]);
assertEquals("and", strings[1]);
assertEquals("foo", strings[2]);
strings = p.split("boo:and:foo", 2);
assertEquals(2, strings.length);
assertEquals("boo", strings[0]);
assertEquals("and:foo", strings[1]);
strings = p.split("boo:and:foo", 5);
assertEquals(3, strings.length);
assertEquals("boo", strings[0]);
assertEquals("and", strings[1]);
assertEquals("foo", strings[2]);
strings = p.split("boo:and:foo", -2);
assertEquals(3, strings.length);
assertEquals("boo", strings[0]);
assertEquals("and", strings[1]);
assertEquals("foo", strings[2]);
p = Pattern.compile("o");
strings = p.split("boo:and:foo");
assertEquals(3, strings.length);
assertEquals("b", strings[0]);
assertEquals("", strings[1]);
assertEquals(":and:f", strings[2]);
strings = p.split("boo:and:foo", 5);
assertEquals(5, strings.length);
assertEquals("b", strings[0]);
assertEquals("", strings[1]);
assertEquals(":and:f", strings[2]);
assertEquals("", strings[3]);
assertEquals("", strings[4]);
strings = p.split("boo:and:foo", -2);
assertEquals(5, strings.length);
assertEquals("b", strings[0]);
assertEquals("", strings[1]);
assertEquals(":and:f", strings[2]);
assertEquals("", strings[3]);
assertEquals("", strings[4]);
strings = p.split("boo:and:foo", 0);
assertEquals(3, strings.length);
assertEquals("b", strings[0]);
assertEquals("", strings[1]);
assertEquals(":and:f", strings[2]);
|
|