List of usage examples for java.util.regex Pattern matcher
public Matcher matcher(CharSequence input)
From source file:Main.java
License:asdf
public static void main(String args[]) { String regex = "\\w+"; Pattern pattern = Pattern.compile(regex); String candidate = "asdf Java2s.com"; Matcher matcher = pattern.matcher(candidate); if (matcher.find()) { System.out.println("GROUP 0:" + matcher.group(0)); }/* w ww .ja v a 2s.co m*/ }
From source file:SimpleSubGroupExample.java
public static void main(String args[]) { Pattern p = Pattern.compile("\\w(\\d)"); String candidate = "J9 is my favorite"; Matcher matcher = p.matcher(candidate); if (matcher.find()) { String tmp = matcher.group(0); System.out.println(tmp);/*from w ww . j av a 2 s. c o m*/ tmp = matcher.group(1); System.out.println(tmp); } }
From source file:Main.java
public static void main(String[] args) { String regex = "[a-z]@."; Pattern p = Pattern.compile(regex); String str = "abc@yahoo.com,123@cnn.com,abc@google.com"; Matcher m = p.matcher(str); if (m.find()) { String foundStr = m.group(); System.out.println("Found text is:" + foundStr); }//from w w w . j av a 2 s . c om }
From source file:GreedyExample.java
public static void main(String args[]) { String regex = "(\\w+)(\\d\\d)(\\w+)"; Pattern pattern = Pattern.compile(regex); String candidate = "X99SuperJava"; Matcher matcher = pattern.matcher(candidate); matcher.find();/*from w w w. ja v a2 s . com*/ System.out.println(matcher.group(1)); System.out.println(matcher.group(2)); System.out.println(matcher.group(3)); }
From source file:ReplaceDemo.java
public static void main(String[] argv) { // Make an RE pattern to match almost any form (deamon, demon, etc.). String patt = "d[ae]{1,2}mon"; // i.e., 1 or 2 'a' or 'e' any combo // A test input. String input = "Unix hath demons and deamons in it!"; System.out.println("Input: " + input); // Run it from a RE instance and see that it works Pattern r = Pattern.compile(patt); Matcher m = r.matcher(input); System.out.println("ReplaceAll: " + m.replaceAll("daemon")); // Show the appendReplacement method m.reset();/*from ww w . j a v a 2 s . c o m*/ StringBuffer sb = new StringBuffer(); System.out.print("Append methods: "); while (m.find()) { m.appendReplacement(sb, "daemon"); // Copy to before first match, // plus the word "daemon" } m.appendTail(sb); // copy remainder System.out.println(sb.toString()); }
From source file:NLMatch.java
public static void main(String[] argv) { String input = "I dream of engines\nmore engines, all day long"; System.out.println("INPUT: " + input); System.out.println();/*from www . java 2 s .c o m*/ String[] patt = { "engines.more engines", "engines$" }; for (int i = 0; i < patt.length; i++) { System.out.println("PATTERN " + patt[i]); boolean found; Pattern p1l = Pattern.compile(patt[i]); found = p1l.matcher(input).find(); System.out.println("DEFAULT match " + found); Pattern pml = Pattern.compile(patt[i], Pattern.DOTALL | Pattern.MULTILINE); found = pml.matcher(input).find(); System.out.println("MultiLine match " + found); System.out.println(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String patternStr = "\\((\\w+)\\)"; String replaceStr = "<$1>"; Pattern pattern = Pattern.compile(patternStr); CharSequence inputStr = "a (b c) d (ef) g"; Matcher matcher = pattern.matcher(inputStr); String output = matcher.replaceAll(replaceStr); }
From source file:PositiveLookaheadExample.java
public static void main(String args[]) { String regex = "(?=^255).*"; Pattern pattern = Pattern.compile(regex); String candidate = "255.0.0.1"; Matcher matcher = pattern.matcher(candidate); String ip = "not found"; if (matcher.find()) ip = matcher.group();/*from w ww . ja va 2 s .co m*/ String msg = "ip: " + ip; System.out.println(msg); }
From source file:Main.java
public static void main(String[] args) { String regex = "\\b(\\d{3})(\\d{3})(\\d{4})\\b"; Pattern p = Pattern.compile(regex); String source = "1234567890, 12345, and 9876543210"; Matcher m = p.matcher(source); while (m.find()) { System.out.println("Phone: " + m.group() + ", Formatted Phone: (" + m.group(1) + ") " + m.group(2) + "-" + m.group(3)); }/*from ww w .j a v a 2 s .c o m*/ }
From source file:MainClass.java
public static void main(String args[]) { String regex = "(\\w+)(\\d\\d)(\\w+)"; Pattern pattern = Pattern.compile(regex); String candidate = "AAA99SuperJava"; Matcher matcher = pattern.matcher(candidate); matcher.find();//from w ww. j a v a 2s . c o m System.out.println(matcher.group(1)); System.out.println(matcher.group(2)); System.out.println(matcher.group(3)); }