Example usage for java.util.regex Matcher start

List of usage examples for java.util.regex Matcher start

Introduction

In this page you can find the example usage for java.util.regex Matcher start.

Prototype

public int start(String name) 

Source Link

Document

Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);

    int nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(nextIndex);

}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is another test";
    Matcher matcher = p.matcher(candidateString);

    matcher.find();/*from  ww w.j  av a2  s.co m*/
    int startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(startIndex);

}

From source file:MatcherStartParamExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("B(ond)");

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "          ^", "           ^", "                      ^",
            "                       ^" };
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from w w w. j  a  v a 2s .  c o  m
    int startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

    int nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

    matcher.find();
    startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[2] + startIndex);

    nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[3] + nextIndex);

}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<postPhoneNumber>\\d{4})\\b";
    String source = "1234567890, 12345, and 9876543210";
    Pattern p = Pattern.compile(regex);

    Matcher m = p.matcher(source);
    while (m.find()) {
        String matchedText = m.group();
        int start1 = m.start("areaCode");
        int start2 = m.start("prefix");
        int start3 = m.start("postPhoneNumber");
        System.out.println("Matched Text:" + matchedText);
        System.out.println("Area code start:" + start1);
        System.out.println("Prefix start:" + start2);
        System.out.println("Line Number start:" + start3);
    }//ww w  .j av a 2s. c  om
}

From source file:ReaderIter.java

public static void main(String[] args) throws IOException {
    // The RE pattern
    Pattern patt = Pattern.compile("[A-Za-z][a-z]+");
    // A FileReader (see the I/O chapter)
    BufferedReader r = new BufferedReader(new FileReader("ReaderIter.java"));

    // For each line of input, try matching in it.
    String line;//from   w  w  w.  j av a2  s .  c  o  m
    while ((line = r.readLine()) != null) {
        // For each match in the line, extract and print it.
        Matcher m = patt.matcher(line);
        while (m.find()) {
            // Simplest method:
            // System.out.println(m.group(0));

            // Get the starting position of the text
            int start = m.start(0);
            // Get ending position
            int end = m.end(0);
            // Print whatever matched.
            // Use CharacterIterator.substring(offset, end);
            System.out.println(line.substring(start, end));
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();

    if (matchFound) {
        // Get all groups for this match
        for (int i = 0; i <= matcher.groupCount(); i++) {
            // Get the group's captured text
            String groupStr = matcher.group(i);

            // Get the group's indices
            int groupStart = matcher.start(i);
            int groupEnd = matcher.end(i);

            // groupStr is equivalent to
            inputStr.subSequence(groupStart, groupEnd);
        }// w  w  w.  java 2s .  co m
    }
}

From source file:RegExTest.java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter pattern: ");
    String patternString = in.nextLine();

    Pattern pattern = null;/*from  w  ww  . jav a2  s  .c  o  m*/
    try {
        pattern = Pattern.compile(patternString);
    } catch (PatternSyntaxException e) {
        System.out.println("Pattern syntax error");
        System.exit(1);
    }

    while (true) {
        System.out.println("Enter string to match: ");
        String input = in.nextLine();
        if (input == null || input.equals(""))
            return;
        Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            System.out.println("Match");
            int g = matcher.groupCount();
            if (g > 0) {
                for (int i = 0; i < input.length(); i++) {
                    for (int j = 1; j <= g; j++)
                        if (i == matcher.start(j))
                            System.out.print('(');
                    System.out.print(input.charAt(i));
                    for (int j = 1; j <= g; j++)
                        if (i + 1 == matcher.end(j))
                            System.out.print(')');
                }
                System.out.println();
            }
        } else
            System.out.println("No match");
    }
}

From source file:com.tek271.reverseProxy.utils.RegexTools.java

private static Tuple3<Integer, Integer, String> getMatch(Matcher matcher, int groupIndex) {
    return Tuple3.tuple3(matcher.start(groupIndex), matcher.end(groupIndex), matcher.group(groupIndex));
}

From source file:de.micromata.genome.gwiki.utils.StringUtils.java

public static String replace(String text, Pattern p, int group,
        CallableX1<String, String, RuntimeException> replacer) {
    if (isEmpty(text) == true) {
        return text;
    }/*from  w w w.  j  a  va2s. com*/
    StringBuilder sb = new StringBuilder();
    Matcher m = p.matcher(text);
    int lastIdx = 0;
    while (m.find() == true) {
        int idx = m.start(group);
        int eidx = m.end(group);
        if (idx > lastIdx) {
            sb.append(text.substring(lastIdx, idx));
        }
        sb.append(replacer.call(text.substring(idx, eidx)));
        lastIdx = eidx;
    }
    if (lastIdx < text.length()) {
        sb.append(text.substring(lastIdx, text.length()));
    }
    return sb.toString();
}

From source file:dk.netarkivet.harvester.harvesting.extractor.ExtractorJS.java

public static long considerStrings(CrawlURI curi, CharSequence cs, CrawlController controller,
        boolean handlingJSFile) {
    long foundLinks = 0;
    Matcher strings = TextUtils.getMatcher(JAVASCRIPT_STRING_EXTRACTOR, cs);
    while (strings.find()) {
        CharSequence subsequence = cs.subSequence(strings.start(2), strings.end(2));

        if (UriUtils.isLikelyUriJavascriptContextLegacy(subsequence)) {
            String string = subsequence.toString();
            string = StringEscapeUtils.unescapeJavaScript(string);
            string = UriUtils.speculativeFixup(string, curi.getUURI());
            foundLinks++;//from   ww w.ja  v  a2 s.  c  o m
            try {
                if (handlingJSFile) {
                    curi.createAndAddLinkRelativeToVia(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                } else {
                    curi.createAndAddLinkRelativeToBase(string, Link.JS_MISC, Link.SPECULATIVE_HOP);
                }
            } catch (URIException e) {
                // There may not be a controller (e.g. If we're being run
                // by the extractor tool).
                if (controller != null) {
                    controller.logUriError(e, curi.getUURI(), string);
                } else {
                    LOGGER.info(curi + ", " + string + ": " + e.getMessage());
                }
            }
        } else {
            foundLinks += considerStrings(curi, subsequence, controller, handlingJSFile);
        }
    }
    TextUtils.recycleMatcher(strings);
    return foundLinks;
}