List of usage examples for java.util.regex Matcher group
public String group(String name)
From source file:REmatch.java
public static void main(String[] argv) { String patt = "Q[^u]\\d+\\."; Pattern r = Pattern.compile(patt); String line = "Order QT300. Now!"; Matcher m = r.matcher(line); if (m.find()) { System.out.println(patt + " matches \"" + m.group(0) + "\" in \"" + line + "\""); } else {/*from ww w.ja va 2 s . co m*/ System.out.println("NO MATCH"); } }
From source file:Main.java
public static void main(String[] args) { String input = " javaId=2 test test javaId=33432. javaId=100"; Pattern p = Pattern.compile("(javaId=)(\\d+)"); Matcher m = p.matcher(input); StringBuffer result = new StringBuffer(); while (m.find()) { System.out.println("Masking: " + m.group(2)); m.appendReplacement(result, m.group(1) + "***masked***"); }/* w ww . ja v a 2s .c o m*/ m.appendTail(result); System.out.println(result); }
From source file:ArrayCreator.java
public static void main(String... args) { Matcher m = p.matcher(s); if (m.find()) { String cName = m.group(1); String[] cVals = m.group(2).split("[\\s,]+"); int n = cVals.length; try {//from w w w.ja va 2s . c o m Class<?> c = Class.forName(cName); Object o = Array.newInstance(c, n); for (int i = 0; i < n; i++) { String v = cVals[i]; Constructor ctor = c.getConstructor(String.class); Object val = ctor.newInstance(v); Array.set(o, i, val); } Object[] oo = (Object[]) o; out.format("%s[] = %s%n", cName, Arrays.toString(oo)); // production code should handle these exceptions more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (NoSuchMethodException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } catch (InstantiationException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); } } }
From source file:GetParen0.java
public static void main(String[] args) { Pattern myRE = Pattern.compile("d.*ian"); Matcher matcher = myRE.matcher("darwinian pterodactyls soared over the devonian space"); matcher.lookingAt();/*from w w w . j av a 2s . c o m*/ String result = matcher.group(0); System.out.println(result); }
From source file:Main.java
public static void main(String[] args) { String string = "var1[value1], var2[value2], var3[value3]"; Pattern pattern = Pattern.compile("(\\[)(.*?)(\\])"); Matcher matcher = pattern.matcher(string); List<String> listMatches = new ArrayList<String>(); while (matcher.find()) { listMatches.add(matcher.group(2)); }// w ww . j a v a2 s. c o m for (String s : listMatches) { System.out.println(s); } }
From source file:Main.java
public static void main(String[] args) { String src = "abc : def"; // two spaces after colon. String tgt = "ghi : jkl"; // three spaces after colon. Pattern spaces = Pattern.compile("([^:]*:)(\\s*)(.*)"); Matcher mSrc = spaces.matcher(src); Matcher mTgt = spaces.matcher(tgt); mSrc.matches();/*w w w. j a va2s . c o m*/ mTgt.matches(); System.out.println("Spaces in src: " + mSrc.group(2).length()); System.out.println("Spaces in tgt: " + mTgt.group(2).length()); System.out.println("Target with src's number of spaces: " + mTgt.group(1) + mSrc.group(2) + mTgt.group(3)); }
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 }/*from w w w. ja v a 2 s . com*/ } }
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 ww w . j av a2 s . c o m } }
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 w w w .j av a 2 s. co m*/ } }
From source file:TheReplacements.java
public static void main(String[] args) throws Exception { String s = TextFile.read("TheReplacements.java"); // Match the specially-commented block of text above: Matcher mInput = Pattern.compile("/\\*!(.*)!\\*/", Pattern.DOTALL).matcher(s); if (mInput.find()) s = mInput.group(1); // Captured by parentheses // Replace two or more spaces with a single space: s = s.replaceAll(" {2,}", " "); // Replace one or more spaces at the beginning of each // line with no spaces. Must enable MULTILINE mode: s = s.replaceAll("(?m)^ +", ""); System.out.println(s);//from w w w .j a v a 2 s. c o m s = s.replaceFirst("[aeiou]", "(VOWEL1)"); StringBuffer sbuf = new StringBuffer(); Pattern p = Pattern.compile("[aeiou]"); Matcher m = p.matcher(s); // Process the find information as you // perform the replacements: while (m.find()) m.appendReplacement(sbuf, m.group().toUpperCase()); // Put in the remainder of the text: m.appendTail(sbuf); System.out.println(sbuf); }