Search in sources :

Example 11 with MatchResult

use of java.util.regex.MatchResult in project robovm by robovm.

the class ScannerTest method test_findWithinHorizon_LPatternI.

/**
     * @tests java.util.Scanner#findWithinHorizon(Pattern, int)
     */
public void test_findWithinHorizon_LPatternI() {
    // 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();
    } catch (IllegalStateException 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();
    } catch (NullPointerException expected) {
    }
    try {
        s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), -1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    s.close();
    try {
        s.findWithinHorizon((Pattern) null, -1);
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("test");
    result = s.findWithinHorizon(Pattern.compile("\\w+"), 10);
    assertEquals("test", result);
    s = new Scanner("aa\n\rb");
    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("^(a)$", Pattern.MULTILINE), 5);
    assertNull(result);
    try {
        mresult = s.match();
        fail();
    } catch (IllegalStateException 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);
    try {
        mresult = s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    assertEquals(0, mresult.groupCount());
    result = s.findWithinHorizon(Pattern.compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"), 100);
    assertEquals("1 fish 2 fish red fish blue", result);
    mresult = s.match();
    assertEquals(4, mresult.groupCount());
    assertEquals("1", mresult.group(1));
    assertEquals("2", mresult.group(2));
    assertEquals("red", mresult.group(3));
    assertEquals("blue", mresult.group(4));
    s = new Scanner("test");
    s.close();
    try {
        s.findWithinHorizon(Pattern.compile("test"), 1);
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("word1 WorD2  ");
    s.close();
    try {
        s.findWithinHorizon(Pattern.compile("\\d+"), 10);
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("word1 WorD2 wOrd3 ");
    Pattern pattern = Pattern.compile("\\d+");
    assertEquals("1", s.findWithinHorizon(pattern, 10));
    assertEquals("WorD2", s.next());
    assertEquals("3", s.findWithinHorizon(pattern, 15));
    // Regression test
    s = new Scanner(new MockStringReader("MockStringReader"));
    pattern = Pattern.compile("test");
    result = s.findWithinHorizon(pattern, 10);
    assertEquals("test", result);
    // Test the situation when input length is longer than buffer size.
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < 1026; i++) {
        stringBuilder.append('a');
    }
    s = new Scanner(stringBuilder.toString());
    pattern = Pattern.compile("\\p{Lower}+");
    result = s.findWithinHorizon(pattern, 1026);
    assertEquals(stringBuilder.toString().length(), result.length());
    assertEquals(stringBuilder.toString(), result);
    // Test the situation when input length is longer than buffer size and
    // set horizon to buffer size.
    stringBuilder = new StringBuilder();
    for (int i = 0; i < 1026; i++) {
        stringBuilder.append('a');
    }
    s = new Scanner(stringBuilder.toString());
    pattern = Pattern.compile("\\p{Lower}+");
    result = s.findWithinHorizon(pattern, 1022);
    assertEquals(1022, result.length());
    assertEquals(stringBuilder.subSequence(0, 1022), result);
    // Test the situation, under which pattern is clipped by buffer.
    stringBuilder = new StringBuilder();
    for (int i = 0; i < 1022; i++) {
        stringBuilder.append(' ');
    }
    stringBuilder.append("bbc");
    assertEquals(1025, stringBuilder.length());
    s = new Scanner(stringBuilder.toString());
    pattern = Pattern.compile("bbc");
    result = s.findWithinHorizon(pattern, 1025);
    assertEquals(3, result.length());
    assertEquals(stringBuilder.subSequence(1022, 1025), result);
    stringBuilder = new StringBuilder();
    for (int i = 0; i < 1026; i++) {
        stringBuilder.append('a');
    }
    s = new Scanner(stringBuilder.toString());
    pattern = Pattern.compile("\\p{Lower}+");
    result = s.findWithinHorizon(pattern, 0);
    assertEquals(stringBuilder.toString(), result);
    stringBuilder = new StringBuilder();
    for (int i = 0; i < 10240; i++) {
        stringBuilder.append('-');
    }
    stringBuilder.replace(0, 2, "aa");
    s = new Scanner(stringBuilder.toString());
    result = s.findWithinHorizon(Pattern.compile("aa"), 0);
    assertEquals("aa", result);
    s = new Scanner("aaaa");
    result = s.findWithinHorizon(Pattern.compile("a*"), 0);
    assertEquals("aaaa", result);
}
Also used : Scanner(java.util.Scanner) Pattern(java.util.regex.Pattern) MatchResult(java.util.regex.MatchResult)

Example 12 with MatchResult

use of java.util.regex.MatchResult in project ratpack by ratpack.

the class TokenPathBinder method bind.

public Optional<PathBinding> bind(PathBinding parentBinding) {
    Matcher matcher = regex.matcher(parentBinding.getPastBinding());
    if (matcher.matches()) {
        MatchResult matchResult = matcher.toMatchResult();
        String boundPath = matchResult.group(1);
        ImmutableMap.Builder<String, String> paramsBuilder = ImmutableMap.builder();
        int i = 2;
        for (String name : tokenNames) {
            String value = matchResult.group(i++);
            if (value != null) {
                paramsBuilder.put(name, decodeURIComponent(value));
            }
        }
        return Optional.of(new DefaultPathBinding(boundPath, paramsBuilder.build(), parentBinding, target));
    } else {
        return Optional.empty();
    }
}
Also used : Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 13 with MatchResult

use of java.util.regex.MatchResult in project es6draft by anba.

the class RegExpPrototype method RegExpReplace.

/**
     * Internal {@code RegExp.prototype.replace()} function.
     * 
     * @param cx
     *            the execution context
     * @param rx
     *            the regular expression object
     * @param string
     *            the string
     * @param replaceValue
     *            the replacement string
     * @return the result string
     */
public static String RegExpReplace(ExecutionContext cx, RegExpObject rx, String string, String replaceValue) {
    /* steps 1-5 (not applicable) */
    /* step 6 */
    int lengthS = string.length();
    /* steps 7-8 (not applicable) */
    /* steps 9-10 */
    boolean global = rx.isSet(RegExpObject.Flags.Global);
    /* step 11 */
    int lastIndex = 0;
    /* steps 12-14 (not applicable) */
    /* step 16 */
    StringBuilder accumulatedResult = new StringBuilder();
    /* step 17 */
    int nextSrcPosition = 0;
    /* steps 15, 19 */
    do {
        /* steps 15.a-15.b */
        MatchResult result = matchResultOrNull(rx, string, lastIndex);
        /* step 15.c */
        if (result == null) {
            break;
        }
        /* step 15.d */
        String matched = result.group(0);
        int matchLength = matched.length();
        lastIndex = (matchLength > 0 ? result.end() : result.end() + 1);
        /* step 15.e (not applicable) */
        /* step 19 */
        int position = result.start();
        assert 0 <= position && position < lengthS;
        assert position >= nextSrcPosition;
        String[] captures = groups(result);
        String replacement = GetSubstitution(matched, string, position, captures, replaceValue);
        accumulatedResult.append(string, nextSrcPosition, position).append(replacement);
        nextSrcPosition = position + matchLength;
    } while (global);
    /* step 20 (not applicable) */
    assert nextSrcPosition <= lengthS;
    /* step 21 */
    return accumulatedResult.append(string, nextSrcPosition, lengthS).toString();
}
Also used : MatchResult(java.util.regex.MatchResult) IterableMatchResult(com.github.anba.es6draft.regexp.IterableMatchResult)

Example 14 with MatchResult

use of java.util.regex.MatchResult in project jdk8u_jdk by JetBrains.

the class TzdbZoneRulesCompiler method parseYear.

private int parseYear(Scanner s, int defaultYear) {
    if (s.hasNext(YEAR)) {
        s.next(YEAR);
        MatchResult mr = s.match();
        if (mr.group(1) != null) {
            // systemv has min
            return 1900;
        } else if (mr.group(2) != null) {
            return YEAR_MAX_VALUE;
        } else if (mr.group(3) != null) {
            return defaultYear;
        }
        return Integer.parseInt(mr.group(4));
    /*
            if (mr.group("min") != null) {
                //return YEAR_MIN_VALUE;
                return 1900;  // systemv has min
            } else if (mr.group("max") != null) {
                return YEAR_MAX_VALUE;
            } else if (mr.group("only") != null) {
                return defaultYear;
            }
            return Integer.parseInt(mr.group("year"));
            */
    }
    throw new IllegalArgumentException("Unknown year: " + s.next());
}
Also used : MatchResult(java.util.regex.MatchResult)

Example 15 with MatchResult

use of java.util.regex.MatchResult in project tesb-rt-se by Talend.

the class CompressionHelper method loadSoapBodyContent.

public static MatchResult loadSoapBodyContent(OutputStream destination, Scanner scanner, String pattern) throws XMLStreamException {
    try {
        scanner.findWithinHorizon(pattern, 0);
        MatchResult result = scanner.match();
        String bodyContent = result.group(CompressionHelper.SOAP_BODY_INDX);
        if (bodyContent != null) {
            destination.write(bodyContent.getBytes());
            return result;
        } else {
            throw new RuntimeException("Compression: Can not find SOAP body");
        }
    } catch (IllegalStateException ex) {
        // TODO: Log message body is not found
        return null;
    } catch (Exception ex) {
        throw new RuntimeException("Compression: can not read SOAP body content", ex);
    } finally {
        if (scanner != null)
            scanner.close();
    }
}
Also used : MatchResult(java.util.regex.MatchResult) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Aggregations

MatchResult (java.util.regex.MatchResult)62 Matcher (java.util.regex.Matcher)26 Pattern (java.util.regex.Pattern)16 Scanner (java.util.Scanner)11 Point (java.awt.Point)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 MatcherState (com.github.anba.es6draft.regexp.MatcherState)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 InputStream (java.io.InputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1