Search in sources :

Example 56 with MatchResult

use of java.util.regex.MatchResult in project flexmark-java by vsch.

the class InlineParserImpl method match.

/**
 * If RE matches at current index in the input, advance index and return the match; otherwise return null.
 *
 * @param re pattern to match
 * @return sequence matched or null
 */
@Override
public BasedSequence match(Pattern re) {
    if (index >= input.length()) {
        return null;
    }
    Matcher matcher = re.matcher(input);
    matcher.region(index, input.length());
    boolean m = matcher.find();
    if (m) {
        index = matcher.end();
        MatchResult result = matcher.toMatchResult();
        return input.subSequence(result.start(), result.end());
    } else {
        return null;
    }
}
Also used : Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Example 57 with MatchResult

use of java.util.regex.MatchResult in project goci by EBISPOT.

the class DataImportExportService method removeEscaping.

/**
 * Removes any escaping present in the given cells, akin to how Excel writes out CSV files surrounding cells with
 * quotation marks. If quotation marks are used to surround some values, and this signifies that they should be used
 * as part of the value supplied; this includes tabs, newlines and other such characters.
 * <p>
 * In 'strict' mode, all such quotation marks are removed from the whole cell.  Strict checking requires regular
 * expression checking, and therefore can fail if the string contains irregular markup (for example, HTML
 * fragments).  Also note that this method will not work with lines that end in an escaped newline character, and
 * you should explicitly remove these characters first using the {@link #endsWithEscapedNewline(String)} and {@link
 * #compensateForEscapedNewlines(String, String)} methods first.
 * <p>
 * In 'relaxed' mode, this method removes quotations ONLY when present at the beginning and end of a cell. This form
 * is different from strict checking in that it does not use regular expressions to check for quoted substrings -
 * rather, it just checks the first and last character of each cell for quotations. Relaxed mode will work with
 * lines that end in an escaped newline character or may not pass regular expression checks.
 * <p>
 *
 * @param cells     the cells on a line, after escaping with quotation marks
 * @param useStrict whether to use the strict definition of escaping (i.e. quotes in any position on a line), or
 *                  whether to consider quotations at the beginning and end of cells only.
 * @return a cell with all quotations removed
 */
public String[] removeEscaping(String[] cells, boolean useStrict) {
    String[] result = new String[cells.length];
    for (int i = 0; i < cells.length; i++) {
        String cell = cells[i];
        if (useStrict) {
            // regex to find quoted substrings
            Pattern p = Pattern.compile("\"[^\"\\r\\n]*\"");
            Matcher m = p.matcher(cell);
            boolean quotedSubstrings = false;
            while (m.find()) {
                MatchResult mr = m.toMatchResult();
                getLog().debug("Found string escaped with quotes, '" + mr.group() + "', removing quotations");
                quotedSubstrings = true;
            }
            // finally, remove all quotes
            if (quotedSubstrings) {
                result[i] = cell.replaceAll("\"", "");
            } else {
                result[i] = cell;
            }
        } else {
            if (cell.startsWith("\"") && cell.endsWith("\"")) {
                result[i] = cell.substring(1, cell.length() - 1);
            } else {
                result[i] = cell;
            }
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Example 58 with MatchResult

use of java.util.regex.MatchResult in project goci by EBISPOT.

the class CatalogSpreadsheetExporter method removeEscaping.

/**
 * Removes any escaping present in the given cells, akin to how Excel writes out CSV files surrounding cells with
 * quotation marks. If quotation marks are used to surround some values, and this signifies that they should be used
 * as part of the value supplied; this includes tabs, newlines and other such characters.
 * <p>
 * In 'strict' mode, all such quotation marks are removed from the whole cell.  Strict checking requires regular
 * expression checking, and therefore can fail if the string contains irregular markup (for example, HTML
 * fragments).  Also note that this method will not work with lines that end in an escaped newline character, and
 * you should explicitly remove these characters first using the {@link #endsWithEscapedNewline(String)} and {@link
 * #compensateForEscapedNewlines(String, String)} methods first.
 * <p>
 * In 'relaxed' mode, this method removes quotations ONLY when present at the beginning and end of a cell. This form
 * is different from strict checking in that it does not use regular expressions to check for quoted substrings -
 * rather, it just checks the first and last character of each cell for quotations. Relaxed mode will work with
 * lines that end in an escaped newline character or may not pass regular expression checks.
 * <p>
 *
 * @param cells     the cells on a line, after escaping with quotation marks
 * @param useStrict whether to use the strict definition of escaping (i.e. quotes in any position on a line), or
 *                  whether to consider quotations at the beginning and end of cells only.
 * @return a cell with all quotations removed
 */
public String[] removeEscaping(String[] cells, boolean useStrict) {
    String[] result = new String[cells.length];
    for (int i = 0; i < cells.length; i++) {
        String cell = cells[i];
        if (useStrict) {
            // regex to find quoted substrings
            Pattern p = Pattern.compile("\"[^\"\\r\\n]*\"");
            Matcher m = p.matcher(cell);
            boolean quotedSubstrings = false;
            while (m.find()) {
                MatchResult mr = m.toMatchResult();
                getLog().debug("Found string escaped with quotes, '" + mr.group() + "', removing quotations");
                quotedSubstrings = true;
            }
            // finally, remove all quotes
            if (quotedSubstrings) {
                result[i] = cell.replaceAll("\"", "");
            } else {
                result[i] = cell;
            }
        } else {
            if (cell.startsWith("\"") && cell.endsWith("\"")) {
                result[i] = cell.substring(1, cell.length() - 1);
            } else {
                result[i] = cell;
            }
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Example 59 with MatchResult

use of java.util.regex.MatchResult in project knotx by Cognifide.

the class HtmlFragmentSplitter method split.

@Override
public List<Fragment> split(String html) {
    List<Fragment> fragments = Lists.newLinkedList();
    if (snippetPatterns.getAnySnippetPattern().matcher(html).matches()) {
        Matcher matcher = snippetPatterns.getSnippetPattern().matcher(html);
        int idx = 0;
        while (matcher.find()) {
            MatchResult matchResult = matcher.toMatchResult();
            if (idx < matchResult.start()) {
                fragments.add(toRaw(html, idx, matchResult.start()));
            }
            fragments.add(toSnippet(matchResult.group(1).intern().split(FragmentConstants.FRAGMENT_IDENTIFIERS_SEPARATOR), html, matchResult.start(), matchResult.end()));
            idx = matchResult.end();
        }
        if (idx < html.length()) {
            fragments.add(toRaw(html, idx, html.length()));
        }
    } else {
        fragments.add(toRaw(html, 0, html.length()));
    }
    return fragments;
}
Also used : Matcher(java.util.regex.Matcher) Fragment(io.knotx.dataobjects.Fragment) MatchResult(java.util.regex.MatchResult)

Example 60 with MatchResult

use of java.util.regex.MatchResult in project logisim-evolution by reds-heig.

the class VhdlParser method parseLine.

private int parseLine(Scanner scanner, StringBuilder type) throws IllegalVhdlContentException {
    if (scanner.findWithinHorizon(Pattern.compile(LINE_PATTERN, Pattern.CASE_INSENSITIVE), 0) == null)
        throw new IllegalVhdlContentException(Strings.get("lineDeclarationException"));
    MatchResult result = scanner.match();
    if (result.groupCount() != 1)
        throw new IllegalVhdlContentException(Strings.get("lineDeclarationException"));
    type.append(getType(result.group(1).toLowerCase()));
    return 1;
}
Also used : 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