示例#1
0
  @Test
  public void wordMatch() {

    // The "/w" just support[a-zA-Z0-9],do not support any unicode
    // characters.

    Matcher matcher = RegExpUtils.getMatcher("[^\\w]*\\w+", "中国");
    assertFalse(matcher.matches());
    matcher = RegExpUtils.getMatcher("[^\\w]*\\w+", "中国abs中国abs");

    List<int[]> l = RegExpUtils.getAllMatchBoundsAsList(matcher);
    assertEquals(l.size(), 2);
    assertEquals(0, ((l.get(0)))[0]);
    assertEquals(5, ((l.get(0)))[1]);
    assertEquals(5, ((l.get(1)))[0]);
    assertEquals(10, ((l.get(1)))[1]);

    Matcher matcher2 = RegExpUtils.getMatcher(".*\\w+", "中国abs中国abs");
    /*
     * The matcher will treat the left most as the highest priority.
     */
    assertTrue(matcher2.find());
    assertEquals(0, matcher2.start());
    assertEquals(10, matcher2.end());
  }
示例#2
0
 /**
  * If you wish one search to pick up where the last one left off you can use the "\G" pattern
  * element. If the string hasn't been searched before, then "\G" matches the beginning of the
  * String.
  *
  * <pre>
  * Regex r = new Regex(&quot;\\Gfoo&quot;);
  * String x = &quot;foofoo foo&quot;;
  * System.out.println(r.search(x));
  * System.out.println(r.search(x));
  * System.out.println(r.search(x));
  * // Prints true, true, false.
  * </pre>
  */
 @Test
 public void testG() {
   Matcher matcher = RegExpUtils.getMatcher("\\G[a-z]", "abcDDde");
   List<int[]> result = RegExpUtils.getAllMatchBoundsAsList(matcher);
   assertEquals(result.size(), 3);
   assertEquals(result.get(0)[0], 0);
   assertEquals(result.get(0)[1], 1);
   assertEquals(result.get(1)[0], 1);
   assertEquals(result.get(1)[1], 2);
   assertEquals(result.get(2)[0], 2);
   assertEquals(result.get(2)[1], 3);
 }