Example usage for java.util.regex Matcher groupCount

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

Introduction

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

Prototype

public int groupCount() 

Source Link

Document

Returns the number of capturing groups in this matcher's pattern.

Usage

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  .  j  a v  a2  s  .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;/*ww w  .j  ava 2s  .  co 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:Main.java

public static long parseDuration(String icalDuration) {
    final int GROUP_SECONDS = 1;
    final String PATTERN = "-?P(\\d)+S";

    long durationInSeconds = -1;

    Pattern p = Pattern.compile(PATTERN);
    Matcher m = p.matcher(icalDuration);
    if (m.groupCount() > 1) {
        durationInSeconds = Long.parseLong(m.group(GROUP_SECONDS));
    }/*from   w w  w .  ja v  a  2s.  c  om*/

    return durationInSeconds;
}

From source file:Main.java

public static String parseOptionalStringAttr(String line, Pattern pattern) {
    Matcher matcher = pattern.matcher(line);
    if (matcher.find() && matcher.groupCount() == 1) {
        return matcher.group(1);
    }//from   ww w .ja v a  2s. c o m
    return null;
}

From source file:Main.java

public static String extract(String response, Pattern p) {
    Matcher matcher = p.matcher(response);
    if (matcher.find() && matcher.groupCount() >= 1) {
        try {// ww w. j ava  2s.  c  om
            return URLDecoder.decode(matcher.group(1), UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    throw new RuntimeException(
            "Response body is incorrect. Can't extract token and secret from this: '" + response + "'", null);

}

From source file:Main.java

/**
 * Extracts a desired string given a string and a pattern using regex
 * //from  w w  w .jav  a2s . c o m
 * @param response the string to extract from
 * @param pattern the regex pattern used to extract 
 * 
 * @return desired extracted string
 */
@SuppressWarnings("deprecation")
public static String extract(String response, Pattern pattern) {
    String extraction = null;
    Matcher matcher = pattern.matcher(response);
    if (matcher.find() && matcher.groupCount() >= 1) {
        extraction = URLDecoder.decode(matcher.group(1));
    }
    return extraction;
}

From source file:Main.java

public static String parseOptionalStringAttr(String paramString, Pattern paramPattern) {
    Matcher localMatcher = paramPattern.matcher(paramString);
    if ((localMatcher.find()) && (localMatcher.groupCount() == 1))
        return localMatcher.group(1);
    return null;/*from  ww  w . ja v a2s  .  c o  m*/
}

From source file:Main.java

/**
 * Match a pattern with a single capturing group and return the content of
 * the capturing group/* ww w  .  j  av  a2  s .co  m*/
 *
 * @param text    the text to match against
 * @param pattern the pattern (regular expression) must contain one and only one
 *                capturing group
 * @return
 */
public static String matchPattern(String text, String pattern) {
    // Use regular expression matching to pull the min and max values from
    // the output of gdalinfo
    Pattern p1 = Pattern.compile(pattern, Pattern.MULTILINE);
    Matcher m1 = p1.matcher(text);
    if (m1.find()) {
        if (m1.groupCount() == 1) {
            return m1.group(1);
        } else {
            throw new RuntimeException("error matching pattern " + pattern);
        }
    } else {
        throw new RuntimeException("error matching pattern " + pattern);
    }
}

From source file:Main.java

public static List<String> matcher(String reg, String text) {
    List<String> matches = new ArrayList<String>();
    Matcher m = Pattern.compile(reg).matcher(text);
    if (m.find()) {
        if (m.groupCount() > 0) {
            for (int i = 1; i <= m.groupCount(); i++) {
                matches.add(m.group(i));
            }/*from  w ww.  j  a  v  a2s . c o  m*/
        }
    }

    return matches;
}

From source file:io.wcm.tooling.netbeans.sightly.completion.classLookup.ParsedStatement.java

/**
 * @param matcher/*from  ww  w .  j av a  2 s  .  co m*/
 * @return Parsed statement
 */
public static ParsedStatement fromMatcher(Matcher matcher) {
    if (matcher.groupCount() >= 5) {
        String command = matcher.group(1);
        String variable = matcher.group(3);
        String value = matcher.group(5);
        if (StringUtils.isNotBlank(command) && StringUtils.isNotBlank(variable)
                && StringUtils.isNotBlank(value)) {
            return new ParsedStatement(command, variable, value);
        }
    }
    return null;
}