List of usage examples for java.util.regex Matcher find
public boolean find()
From source file:Main.java
public static void main(String[] args) throws Exception { String input = "123 321 443 52134 432"; Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(input); int i = 0;//from w w w.ja v a2s . c o m while (matcher.find()) { ++i; } System.out.printf("Matched %d times%n", i); }
From source file:Test.java
public static void main(String[] args) { Pattern p = Pattern.compile("([\\w\\s]+) word"); Matcher m = p.matcher("Could you test a phrase with some word"); while (m.find()) { System.err.println(m.group(1)); System.err.println(m.group()); }/*w w w .j a v a 2s . c om*/ }
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(); 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();//w w w . j ava2 s . c om 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); }
From source file:Main.java
public static void main(final String[] args) { final String[] lines = new String[] { "1. New", "2. UK", "3. France" }; final Pattern p = Pattern.compile("\\.\\s+(.*?)\\s+-\\s+(.*)"); for (final String unparsedText : lines) { String newspaper;// w ww .j av a 2 s . co m String country; final Matcher m = p.matcher(unparsedText); if (m.find()) { newspaper = m.group(1); country = m.group(2); System.out.println("Newspaper: " + newspaper + " Country: " + country); } } }
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)); }// w w w. j a va 2s . com mIdx++; } }
From source file:Main.java
public static void main(String[] args) { String regex = "\\b\\d+\\b"; StringBuffer sb = new StringBuffer(); String replacementText = ""; String matchedText = ""; String text = "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle."; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(text); while (m.find()) { matchedText = m.group();/* w ww.jav a2 s .c om*/ int num = Integer.parseInt(matchedText); if (num == 1) { replacementText = "only one"; } else if (num < 5) { replacementText = "a few"; } else { replacementText = "many"; } m.appendReplacement(sb, replacementText); } m.appendTail(sb); System.out.println("Old Text: " + text); System.out.println("New Text: " + sb.toString()); }
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++;/*ww w .jav a 2 s .com*/ } }
From source file:MainClass.java
public static void main(String[] args) { Matcher m = Pattern.compile("\\w+").matcher("Today is Sunday"); while (m.find()) System.out.println(m.group()); int i = 0;//from w w w .j ava2s . c om while (m.find(i)) { System.out.print(m.group() + " "); i++; } }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "abc\ndef"; String patternStr = "abc$"; // Compile with multiline enabled Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); // true // Use an inline modifier to enable multiline mode matchFound = pattern.matches(".*abc$.*", "abc\r\ndef"); // false matchFound = pattern.matches("(?m).*abc$.*", "abc\r\ndef"); // true }
From source file:ValidationTestWithPatternAndMatcher.java
public static void main(String args[]) { Pattern p = null;// w w w . ja v a 2s. c o m try { p = Pattern.compile("Java \\d"); } catch (PatternSyntaxException pex) { pex.printStackTrace(); System.exit(0); } String candidate = "I love Java 5"; Matcher m = p.matcher(candidate); System.out.println("result=" + m.find()); }