Search in sources :

Example 16 with MatchResult

use of java.util.regex.MatchResult in project openchemlib by Actelion.

the class StringFunctions method extractInverse.

/**
 * @param str
 * @param regex
 * @return the combined not matching parts of the string.
 */
public static String extractInverse(String str, String regex) {
    String substring = "";
    Pattern pa = Pattern.compile(regex);
    Matcher ma = pa.matcher(str);
    if (ma.find()) {
        MatchResult mr = ma.toMatchResult();
        int start = mr.start();
        int end = mr.end();
        String rest1 = str.substring(0, start);
        String rest2 = str.substring(end);
        substring = rest1 + rest2;
    }
    return substring;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult) Point(java.awt.Point)

Example 17 with MatchResult

use of java.util.regex.MatchResult in project openchemlib by Actelion.

the class StringFunctions method formatToCharactersAndDigits.

public static String formatToCharactersAndDigits(String str) {
    String regex = "[0-9a-zA-Z ]";
    Pattern pa = Pattern.compile(regex);
    Matcher ma = pa.matcher(str);
    StringBuilder sb = new StringBuilder();
    int pos = 0;
    while (ma.find(pos)) {
        MatchResult mr = ma.toMatchResult();
        int start = mr.start();
        int end = mr.end();
        pos = end;
        sb.append(str.substring(start, end));
    }
    return sb.toString();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult) Point(java.awt.Point)

Example 18 with MatchResult

use of java.util.regex.MatchResult in project openchemlib by Actelion.

the class StringFunctions method extract.

/**
 * @param str
 * @param regex
 * @return expression which was matched by regex.
 */
public static String extract(String str, String regex) {
    String substring = "";
    Pattern pa = Pattern.compile(regex);
    Matcher ma = pa.matcher(str);
    if (ma.find()) {
        MatchResult mr = ma.toMatchResult();
        substring = mr.group();
    }
    return substring;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Example 19 with MatchResult

use of java.util.regex.MatchResult in project skript-mirror by btk5h.

the class ExprParseRegex method get.

@Override
protected String[] get(Event e) {
    List<MatchResult> regexes = ((CustomSyntaxEvent) e).getParseResult().regexes;
    if (index < regexes.size()) {
        MatchResult match = regexes.get(index);
        int groupCount = match.groupCount();
        String[] groups = new String[groupCount];
        for (int i = 1; i <= groupCount; i++) {
            groups[i - 1] = match.group(i);
        }
        return groups;
    }
    return new String[0];
}
Also used : MatchResult(java.util.regex.MatchResult)

Example 20 with MatchResult

use of java.util.regex.MatchResult in project syndesis by syndesisio.

the class RegexBasedMasqueradeReader method read.

// This overridden method fills sharedBuf with characters read from in.
@Override
@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity", "PMD.StdCyclomaticComplexity", "PMD.NPathComplexity" })
public int read(char[] sharedBuf, int offset, int len) throws IOException {
    int off = offset;
    // Fetch new line if necessary
    if (curLine == null) {
        curLine = ((BufferedReader) in).readLine();
        curLineIx = 0;
    }
    // Return characters from current line
    if (curLine != null) {
        int start = Integer.MAX_VALUE;
        int end = Integer.MAX_VALUE;
        matcher.reset(curLine);
        List<Integer> matches = new ArrayList<>();
        while (matcher.find()) {
            MatchResult matchResult = matcher.toMatchResult();
            matches.add(matchResult.start());
            matches.add(matchResult.end());
        }
        int matchesIdx = 0;
        if (!matches.isEmpty()) {
            start = matches.get(matchesIdx++);
            end = matches.get(matchesIdx++);
        }
        int num = Math.min(len, Math.min(sharedBuf.length - off, curLine.length() - curLineIx));
        // Copy characters from curLine to sharedBuf
        for (int i = 0; i < num; i++) {
            if (curLineIx <= start || curLineIx > end - 1) {
                sharedBuf[off++] = curLine.charAt(curLineIx);
            } else {
                if (curLineIx == end - 1) {
                    if (matchesIdx == matches.size()) {
                        start = Integer.MAX_VALUE;
                        end = Integer.MAX_VALUE;
                    } else {
                        start = matches.get(matchesIdx++);
                        end = matches.get(matchesIdx++);
                    }
                }
                sharedBuf[off++] = '*';
            }
            curLineIx++;
        }
        // No more characters in curLine
        if (curLineIx == curLine.length()) {
            curLine = null;
            // Is there room for the newline?
            if (num < len && off < sharedBuf.length) {
                sharedBuf[off++] = '\n';
                emitNewline = false;
                num++;
            }
        }
        // Return number of character read
        return num;
    } else if (emitNewline && len > 0) {
        // Emit just the newline
        sharedBuf[off] = '\n';
        emitNewline = false;
        return 1;
    } else if (len > 0) {
        // No more characters left in input reader
        return -1;
    } else {
        // Client did not ask for any characters
        return 0;
    }
}
Also used : ArrayList(java.util.ArrayList) MatchResult(java.util.regex.MatchResult)

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