Package java.util.regex

Examples of java.util.regex.MatchResult


        }
       
        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


        // 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());
        // Position 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());
        // Position 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());
        // Position 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());
        // Position 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

        List<Integer[]> hanguls = new ArrayList<Integer[]>();

        while (s.hasNextLine() && s.hasNext()) {
            if (s.findInLine("([^;\\s]*);[^;]*;[^;]*;([^;]*);[^;]*;([^;]*);.*") != null) {
                MatchResult result = s.match();
                int codepoint = Integer.parseInt(result.group(1), 16);
                int cc = Integer.parseInt(result.group(2));
                if (cc != 0) {
                    cc_idx.add(codepoint);
                    cc_data.add(cc);
                }
                String dc = result.group(3).trim();
                if (dc.length() > 0) {
                    if (dc.charAt(0) == '<')
                        compat.set(codepoint);
                    dc = dc.substring(dc.indexOf('>') + 1).trim();
                    String[] points = dc.split("\\s");
View Full Code Here

        List<String> row = null;
       
        Scanner scanner = new Scanner(request.getResponseStream(), "UTF-8");
        while (scanner.hasNextLine()) {
            scanner.findWithinHorizon(CSV_VALUE_PATTERN, 0);
            MatchResult match = scanner.match();
            String quotedString = match.group(2);
            String decoded = quotedString == null ? match.group(1) : quotedString.replaceAll("\"\"", "\"");
           
            if (row == null) {
                row = new ArrayList<String>();
            }
            row.add(decoded);
           
            if (!match.group(4).equals(",")) {
                if (row != null) {
                    rows.add(row);
                    row = 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

        boolean after_def = false;
        boolean value_expected = true;
        TokenType kind = null;

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

            switch (state) {
                case initial:

                    if ((m = source.scan(SPACE)) != null) {
                        encoder.textToken(m.group(), TokenType.space);
                        if (m.group().indexOf("\n") != -1) {
                            import_clause = after_def = false;
                            if (!value_expected) {
                                value_expected = true;
                            }
                        }
                        continue;
                    }
                    else if ((m = source.scan(COMMENT)) != null) {
                        value_expected = true;
                        after_def = false;
                        encoder.textToken(m.group(), TokenType.comment);
                    }
                    else if ((m = source.scan(DOCTYPE)) != null) {
                        encoder.textToken(m.group(), TokenType.doctype);
                    }
                    else if (import_clause && (m = source.scan(INCLUDE)) != null) {
                        after_def = value_expected = false;
                        encoder.textToken(m.group(), TokenType.include);
                    }
                    else if ((m = source.scan(IDENT)) != null) {
                        kind = IDENT_KIND.lookup(m.group());
                        value_expected = (kind == TokenType.keyword) && KEYWORDS_EXPECTING_VALUE.lookup(m.group());
                        if (".".equals(last_token)) {
                            kind = TokenType.ident;
                        }
                        else if (class_name_follows) {
                            kind = TokenType.class_;
                            class_name_follows = false;
                        }
                        else if (after_def && source.check(AFTER_DEF) != null) {
                            kind = TokenType.method;
                            after_def = false;
                        }
                        else if (kind == TokenType.ident && !"?".equals(last_token) && source.check(":") != null) {
                            kind = TokenType.key;
                        }
                        else {
                            if (m.group().equals("class") || (import_clause && m.group().equals("as"))) {
                                class_name_follows = true;
                            }
                            import_clause = (m.group().equals("import"));
                            if (m.group().equals("def")) {
                                after_def = true;
                            }
                        }
                        encoder.textToken(m.group(), kind);
                    }
                    else if ((m = source.scan(SEMI_COLON)) != null) {
                        import_clause = after_def = false;
                        value_expected = true;
                        encoder.textToken(m.group(), TokenType.operator);
                    }
                    else if ((m = source.scan(START_BRACKET)) != null) {
                        class_name_follows = after_def = false;
                        value_expected = true;
                        encoder.textToken(m.group(), TokenType.operator);
                        if (!inlineBlockStack.isEmpty()) {
                            inlineBlockParenDepth += 1;
                        }
                    }
                    else if ((m = source.scan(OPERATOR)) != null) {
                        value_expected = true;
                        //value_expected = :regexp if match == '~';
                        after_def = false;

                        encoder.textToken(m.group(), TokenType.operator);
                    }
                    else if ((m = source.scan(END_BRACKET)) != null) {
                        value_expected = after_def = false;
                        if (!inlineBlockStack.isEmpty() && m.group().equals("}")) {
                            inlineBlockParenDepth -= 1;
                            if (inlineBlockParenDepth == 0) // # closing brace of inline block reached
                            {
                                encoder.textToken(m.group(), TokenType.inline_delimiter);
                                encoder.endGroup(TokenType.inline);
                                Object[] inlineBlock = inlineBlockStack.pop();
                                state = (State) inlineBlock[0];
                                stringDelimiter = (String) inlineBlock[1];
                                inlineBlockParenDepth = (int) inlineBlock[2];
                                continue;
                            }
                        }
                        encoder.textToken(m.group(), TokenType.operator);
                    }
                    else if (source.check(NUMBER) != null) {
                        after_def = value_expected = false;
                        if ((m = source.scan(HEX)) != null) {
                            encoder.textToken(m.group(), TokenType.hex);
                        }
                        else if ((m = source.scan(OCTAL)) != null) {
                            encoder.textToken(m.group(), TokenType.octal);
                        }
                        else if ((m = source.scan(FLOAT)) != null) {
                            encoder.textToken(m.group(), TokenType.float_);
                        }
                        else if ((m = source.scan(INTEGER)) != null) {
                            encoder.textToken(m.group(), TokenType.integer);
                        }
                    }
                    else if ((m = source.scan(MULTI_LINE_DELIMITER)) != null) {
                        after_def = value_expected = false;
                        state = State.multiline_string;
                        encoder.beginGroup(TokenType.string);
                        stringDelimiter = m.group();
                        encoder.textToken(m.group(), TokenType.delimiter);
                    }
                    else if ((m = source.scan(STRING_DELIMITER)) != null) {
                        after_def = value_expected = false;
                        state = m.group().equals("/") ? State.regexp : State.string;
                        encoder.beginGroup(TokenType.valueOf(state.name()));
                        stringDelimiter = m.group();
                        encoder.textToken(m.group(), TokenType.delimiter);
                    }
                    else if (value_expected && (m = source.scan(START_REGEXP)) != null) {
                        after_def = value_expected = false;
                        encoder.beginGroup(TokenType.regexp);
                        state = State.regexp;
                        stringDelimiter = "/";
                        encoder.textToken(m.group(), TokenType.delimiter);
                    }
                    else if ((m = source.scan(ANNOTATION)) != null) {
                        after_def = value_expected = false;
                        encoder.textToken(m.group(), TokenType.annotation);
                    }
                    else if ((m = source.scan(END_OPERATOR)) != null) {
                        after_def = false;
                        value_expected = true;
                        encoder.textToken(m.group(), TokenType.operator);
                    }
                    else {
                        encoder.textToken(source.next(), TokenType.error);
                    }
                    break;

                case string:
                case regexp:
                case multiline_string:

                    if ((m = source.scan(STRING_CONTENT_PATTERN.get(stringDelimiter))) != null) {
                        encoder.textToken(m.group(), TokenType.content);
                    }
                    else if ((m = source.scan(state == State.multiline_string ? "'''|\"\"\"" : "[\"'\\/]")) != null) {
                        encoder.textToken(m.group(), TokenType.delimiter);
                        if (state == State.regexp) {
                            MatchResult modifiers = source.scan("[ix]+");
                            if (modifiers != null && !modifiers.group().equals("")) {
                                encoder.textToken(modifiers.group(), TokenType.modifier);
                            }
                        }
                        if (state == State.multiline_string) {
                            state = State.string;
                        }
View Full Code Here

    @Override
    public void scan(StringScanner source, Encoder encoder, Map<String, Object> options) {
        State state = State.initial;

        while (source.hasMore()) {
            MatchResult m = null;
            switch (state) {
                case initial:
                    if ((m = source.scan(COMMENT)) != null) {
                        encoder.textToken(m.group(), TokenType.comment);
                    }
                    else if ((m = source.scan(SPACE)) != null) {
                        encoder.textToken(m.group(), TokenType.space);
                    }
                    else if ((m = source.scan(KEY)) != null) {
                        encoder.textToken(m.group(), TokenType.key);
                    }
                    else if ((m = source.scan(OPERATOR)) != null) {
                        encoder.textToken(m.group(), TokenType.operator);
                        state = State.value;
                    }
                    else {
                        encoder.textToken(source.next(), TokenType.error);
                    }
                    break;
                case value:
                    if ((m = source.scan(SPACE)) != null) {
                        encoder.textToken(m.group(), TokenType.space);
                    }
                    else if ((m = source.scan(FLOAT)) != null) {
                        encoder.textToken(m.group(), TokenType.float_);
                        state = State.initial;
                    }
                    else if ((m = source.scan(NUMBER)) != null) {
                        String match = m.group();
                        if ((m = source.scan(FLOAT)) != null) {
                            match = match + m.group();
                            encoder.textToken(match, TokenType.float_);
                        }
                        else {
                            encoder.textToken(match, TokenType.integer);
                        }
                        state = State.initial;
                    }
                    else if ((m = source.scan(BOOLEAN)) != null) {
                        encoder.textToken(m.group(), TokenType.value);
                        state = State.initial;
                    }
                    else if ((m = source.scan(UNICODE_ESCAPE)) != null) {
                        encoder.textToken(m.group(), TokenType.value);
                        state = State.initial;
                    }
                    else if ((m = source.scan(VALUE)) != null) {
                        encoder.textToken(m.group(), TokenType.value);
                        if (!m.group().endsWith("\\")) {
                            state = State.initial;
                        }
                    }
                    else {
                        encoder.textToken(source.next(), TokenType.error);
View Full Code Here

        }
        return false;
    }

    private boolean value(StringScanner source, Encoder encoder, Context context) {
        MatchResult m;

        if ((m = source.scan(OPERATOR)) != null) {
            if (context.state == State.colon && (m.group().equals(":") | m.group().equals("-"))) {
                context.state = State.value;
            }
            else if (context.state == State.initial && m.group().equals("-")) {
                context.state = State.value;
            }
            encoder.textToken(m.group(), TokenType.operator);
            return true;
        }
        else if ((m = source.scan(OPERATOR_BRACKETS)) != null) {
            encoder.textToken(m.group(), TokenType.operator);
            return true;
        }
        else if (context.state == State.initial && (m = source.scan(KEY)) != null) {
            encoder.textToken(m.group(), TokenType.key);
            context.key_indent = source.column(source.index() - m.group().length()) - 1;
            context.state = State.colon;
            return true;
        }
        else if ((m = source.scan(KEY_2)) != null) {
            encoder.beginGroup(TokenType.key);
            String match = m.group();
            encoder.textToken(match.substring(0, 1), TokenType.delimiter);
            if (match.length() > 2) {
                encoder.textToken(match.substring(1, match.length() - 1), TokenType.content);
            }
            encoder.textToken(match.substring(match.length() - 1), TokenType.delimiter);
            encoder.endGroup(TokenType.key);
            context.key_indent = source.column(source.index() - match.length()) - 1;
            context.state = State.colon;
            return true;
        }
        else if ((m = source.scan(TYPE_EXP)) != null) {
            encoder.textToken(m.group(1), TokenType.type);
            if (m.group(2) != null) {
                encoder.textToken(":", TokenType.operator);
                encoder.textToken(m.group(3), TokenType.class_);
            }
            return true;
        }
        else if ((m = source.scan(VARIABLE)) != null) {
            encoder.textToken(m.group(), TokenType.variable);
            return true;
        }
        else if ((m = source.scan(GLOBAL_VARIABLE)) != null) {
            encoder.textToken(m.group(), TokenType.global_variable);
            return true;
        }
        else if ((m = source.scan(CLASS_VARIABLE)) != null) {
            encoder.textToken(m.group(), TokenType.class_variable);
            return true;
        }
        else if ((m = source.scan(OCTAL)) != null) {
            encoder.textToken(m.group(), TokenType.octal);
            return true;
        }
        else if ((m = source.scan(OCTAL_2)) != null) {
            encoder.textToken(m.group(), TokenType.octal);
            return true;
        }
        else if ((m = source.scan(SYMBOL)) != null) {
            encoder.textToken(m.group(), TokenType.symbol);
            return true;
        }
        else if ((m = source.scan(ERROR)) != null) {
            encoder.textToken(m.group(), TokenType.error);
            return true;
        }
        else if ((m = source.scan(ERROR_2)) != null) {
            encoder.textToken(m.group(), TokenType.error);
            return true;
        }
        return false;
    }
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.