Search in sources :

Example 91 with Pattern

use of java.util.regex.Pattern in project j2objc by google.

the class Pattern2Test method testCapturingGroups.

public void testCapturingGroups() throws PatternSyntaxException {
    Pattern p;
    Matcher m;
    // Test simple capturing groups
    p = Pattern.compile("(a+)b");
    m = p.matcher("aaaaaaaab");
    assertTrue(m.matches());
    assertEquals(1, m.groupCount());
    assertEquals("aaaaaaaa", m.group(1));
    p = Pattern.compile("((an)+)((as)+)");
    m = p.matcher("ananas");
    assertTrue(m.matches());
    assertEquals(4, m.groupCount());
    assertEquals("ananas", m.group(0));
    assertEquals("anan", m.group(1));
    assertEquals("an", m.group(2));
    assertEquals("as", m.group(3));
    assertEquals("as", m.group(4));
    // Test grouping without capture (?:...)
    p = Pattern.compile("(?:(?:an)+)(as)");
    m = p.matcher("ananas");
    assertTrue(m.matches());
    assertEquals(1, m.groupCount());
    assertEquals("as", m.group(1));
    try {
        m.group(2);
        fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException ioobe) {
    // expected
    }
    // Test combination of grouping and capture
    // TODO
    // Test \<num> sequence with capturing and non-capturing groups
    // TODO
    // Test \<num> with <num> out of range
    p = Pattern.compile("((an)+)as\\1");
    m = p.matcher("ananasanan");
    assertTrue(m.matches());
    try {
        p = Pattern.compile("((an)+)as\\4");
        fail("expected PatternSyntaxException");
    } catch (PatternSyntaxException pse) {
    // expected
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 92 with Pattern

use of java.util.regex.Pattern in project j2objc by google.

the class Pattern2Test method testAnchors.

public void testAnchors() throws PatternSyntaxException {
    Pattern p;
    Matcher m;
    // Test ^, default and MULTILINE
    p = Pattern.compile("^abc\\n^abc", Pattern.MULTILINE);
    m = p.matcher("abc\nabc");
    assertTrue(m.matches());
    p = Pattern.compile("^abc\\n^abc");
    m = p.matcher("abc\nabc");
    assertFalse(m.matches());
// Test $, default and MULTILINE
// TODO
// Test \b (word boundary)
// TODO
// Test \B (not a word boundary)
// TODO
// Test \A (beginning of string)
// TODO
// Test \Z (end of string)
// TODO
// Test \z (end of string)
// TODO
// Test \G
// TODO
// Test positive lookahead using (?=...)
// TODO
// Test negative lookahead using (?!...)
// TODO
// Test positive lookbehind using (?<=...)
// TODO
// Test negative lookbehind using (?<!...)
// TODO
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 93 with Pattern

use of java.util.regex.Pattern in project j2objc by google.

the class Pattern2Test method testRepeats.

public void testRepeats() {
    Pattern p;
    Matcher m;
    // Test ?
    p = Pattern.compile("(abc)?c");
    m = p.matcher("abcc");
    assertTrue(m.matches());
    m = p.matcher("c");
    assertTrue(m.matches());
    m = p.matcher("cc");
    assertFalse(m.matches());
    m = p.matcher("abcabcc");
    assertFalse(m.matches());
    // Test *
    p = Pattern.compile("(abc)*c");
    m = p.matcher("abcc");
    assertTrue(m.matches());
    m = p.matcher("c");
    assertTrue(m.matches());
    m = p.matcher("cc");
    assertFalse(m.matches());
    m = p.matcher("abcabcc");
    assertTrue(m.matches());
    // Test +
    p = Pattern.compile("(abc)+c");
    m = p.matcher("abcc");
    assertTrue(m.matches());
    m = p.matcher("c");
    assertFalse(m.matches());
    m = p.matcher("cc");
    assertFalse(m.matches());
    m = p.matcher("abcabcc");
    assertTrue(m.matches());
    // Test {<num>}, including 0, 1 and more
    p = Pattern.compile("(abc){0}c");
    m = p.matcher("abcc");
    assertFalse(m.matches());
    m = p.matcher("c");
    assertTrue(m.matches());
    p = Pattern.compile("(abc){1}c");
    m = p.matcher("abcc");
    assertTrue(m.matches());
    m = p.matcher("c");
    assertFalse(m.matches());
    m = p.matcher("abcabcc");
    assertFalse(m.matches());
    p = Pattern.compile("(abc){2}c");
    m = p.matcher("abcc");
    assertFalse(m.matches());
    m = p.matcher("c");
    assertFalse(m.matches());
    m = p.matcher("cc");
    assertFalse(m.matches());
    m = p.matcher("abcabcc");
    assertTrue(m.matches());
// Test {<num>,}, including 0, 1 and more
// TODO
// Test {<n1>,<n2>}, with n1 < n2, n1 = n2 and n1 > n2 (illegal?)
// TODO
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 94 with Pattern

use of java.util.regex.Pattern in project j2objc by google.

the class Pattern2Test method testCategory.

private void testCategory(String cat, String... matches) {
    String pa = "{" + cat + "}";
    String pat = "\\p" + pa;
    String npat = "\\P" + pa;
    Pattern p = Pattern.compile(pat);
    Pattern pn = Pattern.compile(npat);
    for (int j = 0; j < matches.length; j++) {
        String t = matches[j];
        boolean invert = t.startsWith("-");
        if (invert) {
            // test negative case, expected to fail
            t = t.substring(1);
            assertFalse("expected '" + t + "' to not be matched " + "by pattern '" + pat, p.matcher(t).matches());
            assertTrue("expected '" + t + "' to  " + "be matched by pattern '" + npat, pn.matcher(t).matches());
        } else {
            assertTrue("expected '" + t + "' to be matched " + "by pattern '" + pat, p.matcher(t).matches());
            assertFalse("expected '" + t + "' to  " + "not be matched by pattern '" + npat, pn.matcher(t).matches());
        }
    }
}
Also used : Pattern(java.util.regex.Pattern)

Example 95 with Pattern

use of java.util.regex.Pattern in project j2objc by google.

the class PatternTest method testAlternations.

public void testAlternations() {
    String baseString = "|a|bc";
    Pattern pat = Pattern.compile(baseString);
    Matcher mat = pat.matcher("");
    assertTrue(mat.matches());
    baseString = "a||bc";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("");
    assertTrue(mat.matches());
    baseString = "a|bc|";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("");
    assertTrue(mat.matches());
    baseString = "a|b|";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("");
    assertTrue(mat.matches());
    baseString = "a(|b|cd)e";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("ae");
    assertTrue(mat.matches());
    baseString = "a(b||cd)e";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("ae");
    assertTrue(mat.matches());
    baseString = "a(b|cd|)e";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("ae");
    assertTrue(mat.matches());
    baseString = "a(b|c|)e";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("ae");
    assertTrue(mat.matches());
    baseString = "a(|)e";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("ae");
    assertTrue(mat.matches());
    baseString = "|";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("");
    assertTrue(mat.matches());
    baseString = "a(?:|)e";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("ae");
    assertTrue(mat.matches());
    baseString = "a||||bc";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("");
    assertTrue(mat.matches());
    baseString = "(?i-is)|a";
    pat = Pattern.compile(baseString);
    mat = pat.matcher("a");
    assertTrue(mat.matches());
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Aggregations

Pattern (java.util.regex.Pattern)3181 Matcher (java.util.regex.Matcher)2116 ArrayList (java.util.ArrayList)387 IOException (java.io.IOException)247 Test (org.junit.Test)238 File (java.io.File)193 HashMap (java.util.HashMap)163 BufferedReader (java.io.BufferedReader)127 Field (java.lang.reflect.Field)119 PatternSyntaxException (java.util.regex.PatternSyntaxException)119 Map (java.util.Map)110 List (java.util.List)93 HashSet (java.util.HashSet)79 InputStreamReader (java.io.InputStreamReader)67 InputStream (java.io.InputStream)43 FileReader (java.io.FileReader)41 FileInputStream (java.io.FileInputStream)40 URL (java.net.URL)35 SmallTest (android.test.suitebuilder.annotation.SmallTest)31 LinkedHashMap (java.util.LinkedHashMap)31