List of usage examples for java.util.regex Matcher group
public String group()
From source file:StartEnd.java
public static void main(String[] args) { String[] input = new String[] { "Java has regular expressions in 1.4", "regular expressions now expressing in Java", "Java represses oracular expressions" }; Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*"); for (int i = 0; i < input.length; i++) { System.out.println("input " + i + ": " + input[i]); Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]); while (m1.find()) System.out.println("m1.find() '" + m1.group() + "' start = " + m1.start() + " end = " + m1.end()); while (m2.find()) System.out.println("m2.find() '" + m2.group() + "' start = " + m2.start() + " end = " + m2.end()); if (m1.lookingAt()) // No reset() necessary System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end()); if (m2.lookingAt()) System.out.println("m2.lookingAt() start = " + m2.start() + " end = " + m2.end()); if (m1.matches()) // No reset() necessary System.out.println("m1.matches() start = " + m1.start() + " end = " + m1.end()); if (m2.matches()) System.out.println("m2.matches() start = " + m2.start() + " end = " + m2.end()); }//from ww w . ja va 2s . c om }
From source file:Main.java
public static void main(String[] args) { String patternStr = "b"; Pattern pattern = Pattern.compile(patternStr); CharSequence inputStr = "a b c b"; Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); System.out.println(matchFound); String match = matcher.group(); System.out.println(match);// ww w. jav a 2s. c o m int start = matcher.start(); int end = matcher.end(); System.out.println(start); System.out.println(end); matchFound = matcher.find(); }
From source file:Main.java
public static void main(String[] args) { String s = "(123, 234; 345, 456) (567, 788; 899, 900)"; Matcher m = Pattern.compile("\\d+").matcher(s); List<Integer> numbers = new ArrayList<Integer>(); while (m.find()) { numbers.add(Integer.parseInt(m.group())); }//from w ww . j av a 2 s . com System.out.println(numbers); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Pattern pattern = Pattern.compile("pattern"); Matcher matcher = pattern.matcher("infile.txt"); // Find all matches while (matcher.find()) { // Get the matching string String match = matcher.group(); }//w ww . ja v a 2 s. c o m }
From source file:MainClass.java
public static void main(String args[]) { String phrase = "a word word"; String duplicatePattern = "\\b(\\w+) \\1\\b"; Pattern p = null;/*from ww w.j a va2 s . co m*/ try { p = Pattern.compile(duplicatePattern); } catch (PatternSyntaxException pex) { pex.printStackTrace(); System.exit(0); } // count the number of matches. int matches = 0; // get the matcher Matcher m = p.matcher(phrase); String val = null; // find all matching Strings while (m.find()) { val = ":" + m.group() + ":"; System.out.println(val); matches++; } }
From source file:Main.java
public static void main(String[] args) { // main method Pattern pattern = Pattern.compile("(?<=sentence).*"); Matcher matcher = pattern.matcher("this is a test!"); boolean found = false; while (matcher.find()) { // if it is found System.out.println("I found the text: " + matcher.group().toString()); found = true;/* w w w . j a va 2 s.co m*/ } if (!found) { // if not System.out.println("I didn't find the text."); // say it wasn't found } }
From source file:Main.java
public static void main(String args[]) { Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); String candidateString = "Java. java JAVA jAVA"; Matcher matcher = p.matcher(candidateString); // display the latter match System.out.println(candidateString); matcher.find(11);//from www .java 2 s .c o m System.out.println(matcher.group()); // display the earlier match System.out.println(candidateString); matcher.find(0); System.out.println(matcher.group()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Create matcher on file Pattern pattern = Pattern.compile("pattern"); Matcher matcher = pattern.matcher(fromFile("infile.txt")); // Find all matches while (matcher.find()) { // Get the matching string String match = matcher.group(); }/*from w w w. j ava 2 s .c o m*/ }
From source file:MainClass.java
public static void main(String[] args) { Pattern p = Pattern.compile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); Matcher m = p.matcher("java has regex\nJava has regex\n" + "JAVA has pretty good regular expressions\n" + "Regular expressions are in Java"); while (m.find()) System.out.println(m.group()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "ab12 cd efg34 123"; String patternStr = "([a-zA-Z]+[0-9]+)"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); StringBuffer buf = new StringBuffer(); boolean found = false; while ((found = matcher.find())) { String replaceStr = matcher.group(); matcher.appendReplacement(buf, "found<" + replaceStr + ">"); }// w w w . j a va 2 s.c o m matcher.appendTail(buf); String result = buf.toString(); System.out.println(result); }