List of usage examples for java.util.regex Matcher groupCount
public int groupCount()
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 numberOfGroups = matcher.groupCount(); System.out.println("numberOfGroups =" + numberOfGroups); }
From source file:MatcherGroupCountExample.java
public static void main(String args[]) { Pattern p = Pattern.compile("B(ond)"); String candidateString = "My name is Bond. James Bond."; Matcher matcher = p.matcher(candidateString); int numberOfGroups = matcher.groupCount(); System.out.println("numberOfGroups =" + numberOfGroups); }
From source file:NonCapturingGroupExample.java
public static void main(String args[]) { String regex = "hello|hi|greetings|(?:good morning)"; String candidate1 = "Java2s say hi to you"; String candidate2 = "Java2s say good morning to you"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(candidate1); System.out.println("GROUP COUNT:" + matcher.groupCount()); if (matcher.find()) System.out.println("GOT 1:" + candidate1); matcher.reset();//from w w w.j ava2s . co m matcher = pattern.matcher(candidate2); System.out.println("GROUP COUNT:" + matcher.groupCount()); if (matcher.find()) System.out.println("GOT 2:" + candidate2); }
From source file:MainClass.java
public static void main(String[] av) { String regEx = "[+|-]?(\\d+(\\.\\d*)?)|(\\.\\d+)"; String str = "a b c d e 1 2 3 4.5 "; Pattern pattern = Pattern.compile(regEx); Matcher m = pattern.matcher(str); while (m.find()) { for (int i = 0; i <= m.groupCount(); i++) { System.out.println("Group " + i + ": " + m.group(i)); // Group i substring }// ww w .j a v a 2 s. c o m } }
From source file:Main.java
public static void main(String[] asd) { String sourcestring = "source string to match with pattern"; Pattern re = Pattern.compile("[^ABC\\s+]"); Matcher m = re.matcher(sourcestring); int mIdx = 0; while (m.find()) { for (int groupIdx = 0; groupIdx < m.groupCount() + 1; groupIdx++) { System.out.println("[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx)); }//from w w w . j av a 2 s . c om mIdx++; } }
From source file:MainClass.java
public static void main(String[] args) { Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(poem); while (m.find()) { for (int j = 0; j <= m.groupCount(); j++) System.out.print("[" + m.group(j) + "]"); System.out.println();//from w w w.java 2 s . c om } }
From source file:Groups.java
public static void main(String[] args) { Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(poem); while (m.find()) { for (int j = 0; j <= m.groupCount(); j++) System.out.print("[" + m.group(j) + "]"); System.out.println();/*from www. j a v a 2 s. com*/ } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String inputStr = "abbabcd"; String patternStr = "(a(?:b*))+(c*)"; // (?:b*) is a non-capturing group Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); if (matchFound) { for (int i = 0; i <= matcher.groupCount(); i++) { String groupStr = matcher.group(i); }/*from w w w .j av a 2 s . com*/ } }
From source file:LogExample.java
public static void main(String argv[]) { String logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\""; System.out.println("Using RE Pattern:"); System.out.println(logEntryPattern); System.out.println("Input line is:"); System.out.println(logEntryLine); Pattern p = Pattern.compile(logEntryPattern); Matcher matcher = p.matcher(logEntryLine); if (!matcher.matches() || NUM_FIELDS != matcher.groupCount()) { System.err.println("Bad log entry (or problem with RE?):"); System.err.println(logEntryLine); return;/*from w w w .j a va2s. c o m*/ } System.out.println("IP Address: " + matcher.group(1)); System.out.println("Date&Time: " + matcher.group(4)); System.out.println("Request: " + matcher.group(5)); System.out.println("Response: " + matcher.group(6)); System.out.println("Bytes Sent: " + matcher.group(7)); if (!matcher.group(8).equals("-")) System.out.println("Referer: " + matcher.group(8)); System.out.println("Browser: " + matcher.group(9)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "abbabcd"; String patternStr = "(a(b*))+(c*)"; // Compile and use regular expression 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++) { String groupStr = matcher.group(i); }//from ww w .j a v a 2s . c o m } }