Package java.util.regex

Examples of java.util.regex.MatchResult


      return TYPE;
   }

   @Override
   public void scan(StringScanner source, Encoder encoder, Map<String, Object> options) {
      MatchResult m = source.scan(ALL);
      if (m != null) {
         encoder.textToken(m.group(), TokenType.plain);
      }
   }
View Full Code Here


        EmbeddedType in_attribute = null;
        String in_tag = null;
        Pattern plain_string_content = null;

        while (source.hasMore()) {
            MatchResult m = null;

            if (state != State.in_special_tag && (m = source.scan(SPACE)) != null) {
                encoder.textToken(m.group(), TokenType.space);
            }
            else {

                switch (state) {
                    case initial:

                        if ((m = source.scan(CDATA_START)) != null) {
                            encoder.textToken(m.group(), TokenType.inline_delimiter);
                            if ((m = source.scan(CDATA_END)) != null) {
                                encoder.textToken(m.group().substring(0, m.group().length() - 3), TokenType.plain);
                                encoder.textToken("]]>", TokenType.inline_delimiter);
                            }
                            else if ((m = source.scan(CDATA_ERROR)) != null) {
                                encoder.textToken(m.group(), TokenType.error);
                            }
                        }
                        else if ((m = source.scan(COMMENT)) != null) {
                            encoder.textToken(m.group(), TokenType.comment);
                        }
                        else if ((m = source.scan(DOCTYPE)) != null) {
                            encoder.textToken(m.group(), TokenType.doctype);
                        }
                        else if ((m = source.scan(PRE_PROCESSOR)) != null) {
                            encoder.textToken(m.group(), TokenType.preprocessor);
                        }
                        else if ((m = source.scan(COMMENT2)) != null) {
                            encoder.textToken(m.group(), TokenType.comment);
                        }
                        else if ((m = source.scan(TAG)) != null) {
                            in_tag = null;
                            encoder.textToken(m.group(), TokenType.tag);
                        }
                        else if ((m = source.scan(SPECIAL_TAG)) != null) {
                            encoder.textToken(m.group(), TokenType.tag);
                            in_tag = m.group(1);
                            if (m.group(2) != null) {
                                if (in_tag != null) {
                                    state = State.in_special_tag;
                                }
                            }
                            else {
                                state = State.attribute;
                            }
                        }
                        else if ((m = source.scan(PLAIN)) != null) {
                            encoder.textToken(m.group(), TokenType.plain);
                        }
                        else if ((m = source.scan(ENTITY)) != null) {
                            encoder.textToken(m.group(), TokenType.entity);
                        }
                        else if ((m = source.scan(ERROR)) != null) {
                            in_tag = null;
                            encoder.textToken(m.group(), TokenType.error);
                        }
                        else {
                            throw new RuntimeException("[BUG] else-case reached with state " + state + " in " + getClass());
                        }

                        break;
                    case attribute:

                        if ((m = source.scan(TAG_END)) != null) {
                            encoder.textToken(m.group(), TokenType.tag);
                            in_attribute = null;
                            if (in_tag != null) {
                                state = State.in_special_tag;
                            }
                            else {
                                state = State.initial;
                            }
                        }
                        else if ((m = source.scan(ATTR_NAME)) != null) {
                            in_attribute = IN_ATTRIBUTE.lookup(m.group());
                            encoder.textToken(m.group(), TokenType.attribute_name);
                            state = State.attribute_equal;
                        }
                        else {
                            in_tag = null;
                            encoder.textToken(source.next(), TokenType.error);
                        }

                        break;
                    case attribute_equal:

                        if ((m = source.scan(EQUAL)) != null) {
                            encoder.textToken(m.group(), TokenType.operator);
                            state = State.attribute_value;
                        }
                        else {
                            state = State.attribute;
                            break;
                        }

                    case attribute_value:
                        if ((m = source.scan(ATTR_NAME)) != null) {
                            encoder.textToken(m.group(), TokenType.attribute_value);
                            state = State.attribute;
                        }
                        else if ((m = source.scan(QUOTE)) != null) {
                            if (EmbeddedType.script == in_attribute || EmbeddedType.style == in_attribute) {
                                encoder.beginGroup(TokenType.string);
                                encoder.textToken(m.group(), TokenType.delimiter);
                                String groupStart = m.group();

                                if ((m = source.scan(JAVASCRIPT_INLINE)) != null) {
                                    encoder.textToken(m.group(), TokenType.comment);
                                }
                                String code = source.scanUntil(Pattern.compile("(?=" + groupStart + "|\\z)")).group();
                                if (EmbeddedType.script == in_attribute) {
                                    Syntax.Builder.create()
                                            .scannerType(JavaScriptScanner.TYPE.getName())
                                            .encoder(encoder)
                                            .execute(code);
                                }
                                else {
                                    Syntax.Builder.create()
                                            .scannerType(CSSScanner.TYPE.getName())
                                            .encoder(encoder)
                                            .scannerOptions(
                                                    Options.create()
                                                            .add(CSSScanner.OPTION_START_STATE, CSSScanner.State.block)
                                            )
                                            .execute(code);
                                }
                                m = source.scan(QUOTE);
                                if (m != null) {
                                    encoder.textToken(m.group(), TokenType.delimiter);
                                }
                                encoder.endGroup(TokenType.string);
                                state = State.attribute;
                                in_attribute = null;
                            }
                            else {
                                encoder.beginGroup(TokenType.string);
                                state = State.attribute_value_string;
                                plain_string_content = PLAIN_STRING_CONTENT.get(m.group());
                                encoder.textToken(m.group(), TokenType.delimiter);
                            }
                        }
                        else if ((m = source.scan(TAG_END)) != null) {
                            encoder.textToken(m.group(), TokenType.tag);
                            state = State.initial;
                        }
                        else {
                            encoder.textToken(source.next(), TokenType.error);
                        }
                        break;
                    case attribute_value_string:

                        if ((m = source.scan(plain_string_content)) != null) {
                            encoder.textToken(m.group(), TokenType.content);
                        }
                        else if ((m = source.scan(QUOTE)) != null) {
                            encoder.textToken(m.group(), TokenType.delimiter);
                            encoder.endGroup(TokenType.string);
                            state = State.attribute;
                        }
                        else if ((m = source.scan(ENTITY)) != null) {
                            encoder.textToken(m.group(), TokenType.entity);
                        }
                        else if ((m = source.scan(AMP)) != null) {
                            encoder.textToken(m.group(), TokenType.content);
                        }
                        else if ((m = source.scan(END)) != null) {
                            encoder.endGroup(TokenType.string);
                            state = State.initial;
                            encoder.textToken(m.group(), TokenType.error);
                        }
                        break;
                    case in_special_tag:

                        if ("script".equalsIgnoreCase(in_tag) || "style".equalsIgnoreCase(in_tag)) {
                            String code = null;
                            String closing = null;
                            if ((m = source.scan(SPECIAL_SPACE)) != null) {
                                encoder.textToken(m.group(), TokenType.space);
                            }
                            if ((m = source.scan(SPECIAL_COMMENT)) != null) {
                                code = m.group(2);
                                if (code == null) {
                                    code = m.group(4);
                                }
                                closing = m.group(3);
                                encoder.textToken(m.group(1), TokenType.comment);
                            }
                            else {
                                code = source.scanUntil("(?=(?:\\n\\s*)?<\\/" + in_tag + ">)|\\z").group();
                                closing = null;
                            }
View Full Code Here

    }

    public MatchResult scan(Pattern pattern) {
        Matcher m = pattern.matcher(sequence);
        if (m.lookingAt()) {
            MatchResult result = new StaticMatchResult(sequence, m);
            sequence.advance(m.end());
            return result;
        }
        return null;
    }
View Full Code Here

    }

    public MatchResult scanUntil(Pattern pattern) {
        Matcher m = pattern.matcher(sequence);
        if (m.find()) {
            MatchResult result = new UntilStaticMatchResult(sequence, m);
            sequence.advance(m.end());
            return result;
        }
        return null;
    }
View Full Code Here

    /**
     * @tests java.util.Scanner#match()
     */
    public void test_match() {
        MatchResult result ;
        s = new Scanner("1 2 ");
        try {
            s.match();
            fail("should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // Expected
        }
        assertEquals("1", s.next());
        assertEquals("2", s.next());
        result = s.match();
        assertEquals(2, result.start());
        assertEquals(3, result.end());
        assertEquals(2, result.start(0));
        assertEquals(3, result.end(0));
        assertEquals("2", result.group());
        assertEquals("2", result.group(0));
        assertEquals(0, result.groupCount());
        try {
            result.start(1);
            fail("should throw IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            // Expected
        }
        try {
            s.next();
            fail("should throw NoSuchElementException");
        } catch (NoSuchElementException e) {
            // Expected
        }
        try {
            s.match();
            fail("should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // Expected
        }
       
        s = new Scanner("True faLse");
        try {
            s.match();
            fail("should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // Expected
        }
        assertTrue(s.nextBoolean());
        result = s.match();
        assertEquals(0, result.start());
        assertEquals(4, result.end());
        assertEquals(0, result.start(0));
        assertEquals(4, result.end(0));
        assertEquals("True", result.group());
        assertEquals(0, result.groupCount());
        assertFalse(s.nextBoolean());
        try {
            s.nextBoolean();
            fail("should throw NoSuchElementException");
        } catch (NoSuchElementException e) {
            // Expected
        }
        try {
            s.match();
            fail("should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // Expected
        }
       
        s = new Scanner("True faLse");
        assertTrue(s.nextBoolean());
        result = s.match();
        assertEquals(0, result.start());
        assertEquals(4, result.end());
        assertEquals(0, result.start(0));
        assertEquals(4, result.end(0));
        assertEquals("True", result.group());
        assertEquals(0, result.groupCount());
        s.close();
        try {
            s.nextBoolean();
            fail("should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // Expected
        }
        result = s.match();
        assertEquals(0, result.start());
        assertEquals(4, result.end());
        assertEquals(0, result.start(0));
        assertEquals(4, result.end(0));
        assertEquals("True", result.group());
        assertEquals(0, result.groupCount());
       
        s = new Scanner("True fase");
        assertTrue(s.nextBoolean());
        assertEquals(0, result.groupCount());
        try {
            s.nextBoolean();
            fail("Should throw InputMismatchException");
        } catch (InputMismatchException e) {
            // expected
        }
        try {
            s.match();
            fail("should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // Expected
        }
       
        s = new Scanner("True fase");
        assertTrue(s.nextBoolean());
        try {
            s.next((Pattern)null);
            fail("Should throw NullPointerException");
        } catch (NullPointerException e) {
            // Expected
        }
        result = s.match();
        assertEquals(0, result.start());
        assertEquals(4, result.end());
        assertEquals(0, result.start(0));
        assertEquals(4, result.end(0));
        assertEquals("True", result.group());
        assertEquals(0, result.groupCount());
       
    }
View Full Code Here

        // This method searches through the input up to the specified search
        // horizon(exclusive).
        s = new Scanner("123test");
        String result = s.findWithinHorizon(Pattern.compile("\\p{Lower}"), 5);
        assertEquals("t", result);
        MatchResult mresult = s.match();
        assertEquals(3, mresult.start());
        assertEquals(4, mresult.end());

        s = new Scanner("12345test1234test next");
        /*
         * If the pattern is found the scanner advances past the input that
         * matched and returns the string that matched the pattern.
         */
        result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 2);
        assertEquals("12", result);
        mresult = s.match();
        assertEquals(0, mresult.start());
        assertEquals(2, mresult.end());
        // Postion is now pointing at the bar. "12|345test1234test next"

        result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 6);
        assertEquals("345", result);

        mresult = s.match();
        assertEquals(2, mresult.start());
        assertEquals(5, mresult.end());
        // Postion is now pointing at the bar. "12345|test1234test next"

        // If no such pattern is detected then the null is returned and the
        // scanner's position remains unchanged.
        result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 3);
        assertNull(result);

        try {
            s.match();
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }
        assertEquals("345", mresult.group());
        assertEquals(2, mresult.start());
        assertEquals(5, mresult.end());
        // Postion is now still pointing at the bar. "12345|test1234test next"

        // If horizon is 0, then the horizon is ignored and this method
        // continues to search through the input looking for the specified
        // pattern without bound.
        result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 0);
        mresult = s.match();
        assertEquals(9, mresult.start());
        assertEquals(13, mresult.end());
        // Postion is now pointing at the bar. "12345test1234|test next"

        assertEquals("test", s.next());
        mresult = s.match();
        assertEquals(13, mresult.start());
        assertEquals(17, mresult.end());

        assertEquals("next", s.next());
        mresult = s.match();
        assertEquals(18, mresult.start());
        assertEquals(22, mresult.end());

        try {
            s.findWithinHorizon((Pattern) null, -1);
            fail("Should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected
        }

        try {
            s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), -1);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }

        s.close();
        try {
            s.findWithinHorizon((Pattern) null, -1);
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }

        s = new Scanner("test");
        result = s.findWithinHorizon(Pattern.compile("\\w+"), 10);
        assertEquals("test", result);

        s = new Scanner("aa\n\rb");
        String patternStr = "^(a)$";
        result = s.findWithinHorizon(Pattern.compile("a"), 5);
        assertEquals("a", result);
        mresult = s.match();
        assertEquals(0, mresult.start());
        assertEquals(1, mresult.end());

        result = s.findWithinHorizon(Pattern.compile(patternStr,
                Pattern.MULTILINE), 5);
        assertNull(result);

        try {
            mresult = s.match();
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }

        s = new Scanner("");
        result = s.findWithinHorizon(Pattern.compile("^"), 0);
        assertEquals("", result);
        MatchResult matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(0, matchResult.end());

        result = s.findWithinHorizon(Pattern.compile("$"), 0);
        assertEquals("", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(0, matchResult.end());

        s = new Scanner("1 fish 2 fish red fish blue fish");
        result = s.findWithinHorizon(Pattern
                .compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"), 10);
        assertNull(result);
View Full Code Here

        } catch (NullPointerException e) {
            // Expected
        }
        String result = s.findInLine(Pattern.compile("^"));
        assertEquals("", result);
        MatchResult matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(0, matchResult.end());

        result = s.findInLine(Pattern.compile("$"));
        assertEquals("", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(0, matchResult.end());

        /*
         * When we use the operation of findInLine(Pattern), the match region
         * should not span the line separator.
         */
        s = new Scanner("aa\nb.b");
        result = s.findInLine(Pattern.compile("a\nb*"));
        assertNull(result);

        s = new Scanner("aa\nbb.b");
        result = s.findInLine(Pattern.compile("\\."));
        assertNull(result);

        s = new Scanner("abcd1234test\n");
        result = s.findInLine(Pattern.compile("\\p{Lower}+"));
        assertEquals("abcd", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(4, matchResult.end());

        result = s.findInLine(Pattern.compile("\\p{Digit}{5}"));
        assertNull(result);
        try {
            matchResult = s.match();
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }
        assertEquals(0, matchResult.start());
        assertEquals(4, matchResult.end());

        result = s.findInLine(Pattern.compile("\\p{Lower}+"));
        assertEquals("test", result);
        matchResult = s.match();
        assertEquals(8, matchResult.start());
        assertEquals(12, matchResult.end());

        char[] chars = new char[2048];
        Arrays.fill(chars, 'a');
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(chars);
        stringBuilder.append("1234");
        s = new Scanner(stringBuilder.toString());
        result = s.findInLine(Pattern.compile("\\p{Digit}+"));
        assertEquals("1234", result);
        matchResult = s.match();
        assertEquals(2048, matchResult.start());
        assertEquals(2052, matchResult.end());

        s = new Scanner("test");
        s.close();
        try {
            s.findInLine((Pattern) null);
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }

        s = new Scanner("test1234\n1234 test");
        result = s.findInLine(Pattern.compile("test"));
        assertEquals("test", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(4, matchResult.end());

        int number = s.nextInt();
        assertEquals(1234, number);
        matchResult = s.match();
        assertEquals(4, matchResult.start());
        assertEquals(8, matchResult.end());

        result = s.next();
        assertEquals("1234", result);
        matchResult = s.match();
        assertEquals(9, matchResult.start());
        assertEquals(13, matchResult.end());

        result = s.findInLine(Pattern.compile("test"));
        assertEquals("test", result);
        matchResult = s.match();
        assertEquals(14, matchResult.start());
        assertEquals(18, matchResult.end());
       
        s = new Scanner("test\u0085\ntest");
        result = s.findInLine("est");
        assertEquals("est", result);
        result = s.findInLine("est");
View Full Code Here

        } catch (IllegalStateException e) {
            // expected
        }

        s.skip(Pattern.compile("\\p{Digit}"));
        MatchResult matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());

        s.skip(Pattern.compile("\\p{Digit}+"));
        matchResult = s.match();
        assertEquals(1, matchResult.start());
        assertEquals(4, matchResult.end());

        s.close();
        try {
            s.skip(Pattern.compile("test"));
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }

        MockStringReader2Read reader = new MockStringReader2Read("test");
        s = new Scanner(reader);
        try {
            s.skip(Pattern.compile("\\p{Digit}{4}"));
            fail("Should throw NoSuchElementException");
        } catch (NoSuchElementException e) {
            // expected
        }
        try {
            s.match();
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }
        s.skip(Pattern.compile("\\p{Digit}{3}\\p{Lower}"));
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(4, matchResult.end());

        s.close();
        try {
            s.skip((Pattern) null);
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }
       
        StringBuilder stringBuilder = new StringBuilder();
        char [] chars = new char[1024];
        Arrays.fill(chars, 'a');
        stringBuilder.append(chars);
        stringBuilder.append('3');
        s = new Scanner(stringBuilder.toString());
        s.skip(Pattern.compile("\\p{Lower}+\\p{Digit}"));
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1025, matchResult.end());
       
        // Large amount of input may be cached
        chars = new char[102400];
        Arrays.fill(chars, 'a');
        stringBuilder = new StringBuilder();
        stringBuilder.append(chars);
        s = new Scanner(stringBuilder.toString());
        s.skip(Pattern.compile(".*"));
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(102400, matchResult.end());
       
        // skip something without risking a NoSuchElementException
        s.skip(Pattern.compile("[ \t]*"));
        matchResult = s.match();
        assertEquals(102400, matchResult.start());
        assertEquals(102400, matchResult.end());
    }
View Full Code Here

        }
       
        s = new Scanner("test\r\ntest");
        String result = s.nextLine();
        assertEquals("test", result);
        MatchResult matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(6, matchResult.end());

        s = new Scanner("\u0085");
        result = s.nextLine();
        assertEquals("", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
       
        s = new Scanner("\u2028");
        result = s.nextLine();
        assertEquals("", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
       
        s = new Scanner("\u2029");
        result = s.nextLine();
        assertEquals("", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
       
        s = new Scanner("");
        try {
            result = s.nextLine();
            fail("Should throw NoSuchElementException");
        } catch (NoSuchElementException e) {
            // expected
        }
        try {
            s.match();
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected
        }

        s = new Scanner("Ttest");
        result = s.nextLine();
        assertEquals("Ttest", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(5, matchResult.end());

        s = new Scanner("\r\n");
        result = s.nextLine();
        assertEquals("", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(2, matchResult.end());

        char[] chars = new char[1024];
        Arrays.fill(chars, 'a');
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(chars);
        chars = new char[] { '+', '-' };
        stringBuilder.append(chars);
        stringBuilder.append("\u2028");
        s = new Scanner(stringBuilder.toString());
        result = s.nextLine();

        assertEquals(stringBuilder.toString().substring(0, 1026), result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1027, matchResult.end());

        chars = new char[1023];
        Arrays.fill(chars, 'a');
        stringBuilder = new StringBuilder();
        stringBuilder.append(chars);
        stringBuilder.append("\r\n");
        s = new Scanner(stringBuilder.toString());
        result = s.nextLine();

        assertEquals(stringBuilder.toString().substring(0, 1023), result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1025, matchResult.end());

        s = new Scanner("  ");
        result = s.nextLine();
        assertEquals("  ", result);

        s = new Scanner("test\n\n\n");
        result = s.nextLine();
        assertEquals("test", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(5, matchResult.end());
        result = s.nextLine();
        matchResult = s.match();
        assertEquals(5, matchResult.start());
        assertEquals(6, matchResult.end());

        s = new Scanner("\n\n\n");
        result = s.nextLine();
        assertEquals("", result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
        result = s.nextLine();
        matchResult = s.match();
        assertEquals(1, matchResult.start());
        assertEquals(2, matchResult.end());
       
        s = new Scanner("123 test\n   ");
        int value = s.nextInt();
        assertEquals(123, value);
       
View Full Code Here

        }
       
        s = new Scanner("test\r\ntest");
        boolean result = s.hasNextLine();
        assertTrue(result);
        MatchResult matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(6, matchResult.end());

        s = new Scanner("\u0085");
        result = s.hasNextLine();
        assertTrue(result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
       
        s = new Scanner("\u2028");
        result = s.hasNextLine();
        assertTrue(result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
       
        s = new Scanner("\u2029");
        result = s.hasNextLine();
        assertTrue(result);
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
       
        s = new Scanner("test\n");
        assertTrue(s.hasNextLine());
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(5, matchResult.end());

        char[] chars = new char[2048];
        Arrays.fill(chars, 'a');
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(chars);
        s = new Scanner(stringBuilder.toString());
        result = s.hasNextLine();
        assertTrue(result);

        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(2048, matchResult.end());

        s = new Scanner("\n\n\n");
        assertTrue(s.hasNextLine());
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());

        // The scanner will not advance any input.
        assertTrue(s.hasNextLine());
        matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
    }
View Full Code Here

TOP

Related Classes of java.util.regex.MatchResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.