List of usage examples for java.util.regex Pattern matcher
public Matcher matcher(CharSequence input)
From source file:MatcherFindExample.java
public static void main(String args[]) { Pattern p = Pattern.compile("Java"); String candidateString = "I love Java2s. Java2s is about Java."; Matcher matcher = p.matcher(candidateString); while (matcher.find()) { System.out.println(matcher.group()); }/* ww w .j a v a 2s .c o m*/ }
From source file:Main.java
public static void main(String args[]) throws Exception { String duplicatePattern = "\\b(\\w+) \\1\\b"; Pattern p = Pattern.compile(duplicatePattern); int matches = 0; String phrase = "this is a test"; Matcher m = p.matcher(phrase); String val = null; while (m.find()) { val = ":" + m.group() + ":"; System.out.println(val); matches++;//w w w .j a v a2 s . com } }
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 w w w. j a v a2 s.com*/ System.out.println("NO MATCH"); } }
From source file:MatcherReplaceFirstExample.java
public static void main(String args[]) { Pattern p = Pattern.compile("(i|I)ce"); String candidateString = "I love ice. Ice is my favorite. Ice Ice Ice."; Matcher matcher = p.matcher(candidateString); String tmp = matcher.replaceFirst("Java"); System.out.println(tmp);//from w w w. j av a 2 s. c om }
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); // Find group number 0 of the first find matcher.find();//from w ww .j ava 2s .c o m String group_0 = matcher.group(0); String group_1 = matcher.group(1); System.out.println("Group 0 " + group_0); System.out.println("Group 1 " + group_1); System.out.println(candidateString); }
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 ava2 s . co 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); }
From source file:MainClass.java
public static void main(String args[]) { Pattern pat; Matcher mat;/*from w w w . j a v a 2 s. c o m*/ boolean found; pat = Pattern.compile("Java"); mat = pat.matcher("Java"); found = mat.matches(); if (found) System.out.println("Matches"); else System.out.println("No Match"); System.out.println(); mat = pat.matcher("Java 2"); found = mat.matches(); if (found) System.out.println("Matches"); else System.out.println("No Match"); }
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;//ww w. j av a 2 s . 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:MainClass.java
public static void main(String[] args) { String pattStr = "\u00e9gal"; // gal String[] input = { "\u00e9gal", // gal - this one had better match :-) "e\u0301gal", // e + "Combining acute accent" "e\u02cagal", // e + "modifier letter acute accent" "e'gal", // e + single quote "e\u00b4gal", // e + Latin-1 "acute" };//from ww w . j a v a2 s . co m Pattern pattern = Pattern.compile(pattStr, Pattern.CANON_EQ); for (int i = 0; i < input.length; i++) { if (pattern.matcher(input[i]).matches()) { System.out.println(pattStr + " matches input " + input[i]); } else { System.out.println(pattStr + " does not match input " + input[i]); } } }
From source file:MatcherGroupParamExample.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); matcher.find();// w w w . ja v a2 s. c o m String group_0 = matcher.group(0); String group_1 = matcher.group(1); System.out.println("Group 0 " + group_0); System.out.println("Group 1 " + group_1); System.out.println(candidateString); matcher.find(); group_0 = matcher.group(0); group_1 = matcher.group(1); System.out.println("Group 0 " + group_0); System.out.println("Group 1 " + group_1); System.out.println(candidateString); }