Example usage for java.util.regex Matcher end

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

Introduction

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

Prototype

public int end(String name) 

Source Link

Document

Returns the offset after the last character of the subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:MatcherEndParamExample.java

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

    String candidateString = "My name is Bond. James Bond.";

    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);

    matcher.find();//from w w w  . ja va 2 s .c  om
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + endIndex);

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

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

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

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);

    // find the end point of the second sub group (ond)
    int nextIndex = matcher.end(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[3] + nextIndex);

}

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);
    // Find the end point of the first 'B(ond)'
    matcher.find();/*  ww w.j  a va2  s .co  m*/
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + endIndex);

}

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);

    // Find the end point of the second 'B(ond)'
    matcher.find();/*from w w w  .  j  a v a 2s  .com*/
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[2] + endIndex);

}

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from  ww  w  . j  av a 2 s .  c  om
    //  find the end point of the first sub group (ond)
    int nextIndex = matcher.end(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

}

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  . ja  va  2  s  .  c  om
    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);
        }//from   w w w  .j  a va  2 s  .com
    }
}

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 ww  w.  j  ava  2  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:com.ejisto.core.classloading.util.ReflectionUtils.java

private static String extractFieldName(String methodName) {
    Matcher m = FIELD_EXTRACTOR.matcher(methodName);
    if (!m.matches()) {
        return null;
    }//  w  w  w .  j  a  va2 s.  c  o  m
    return uncapitalize(methodName.substring(m.end(1)));
}