List of usage examples for java.util.regex Pattern compile
public static Pattern compile(String regex)
From source file:Main.java
License:asdf
public static void main(String args[]) { String regex = "(\\w+)(\\d\\d)(\\w+)"; Pattern pattern = Pattern.compile(regex); String candidate = "X99 asdf 44"; Matcher matcher = pattern.matcher(candidate); matcher.find();/*from w ww . j a v a2s. c o m*/ System.out.println(matcher.group(1)); System.out.println(matcher.group(2)); System.out.println(matcher.group(3)); }
From source file:MatcherEndParamExample.java
public static void main(String args[]) { Pattern p = Pattern.compile("B(on)d"); String candidateString = "My name is Bond. James Bond."; String matchHelper[] = { " ^", " ^", " ^", " ^" }; Matcher matcher = p.matcher(candidateString); matcher.find();//ww w .ja v a 2 s . c o m int endIndex = matcher.end(0); System.out.println(candidateString); System.out.println(matchHelper[0] + endIndex); int nextIndex = matcher.end(1); System.out.println(candidateString); System.out.println(matchHelper[1] + nextIndex); matcher.find(); endIndex = matcher.end(0); System.out.println(candidateString); System.out.println(matchHelper[2] + endIndex); nextIndex = matcher.end(1); System.out.println(candidateString); System.out.println(matchHelper[3] + nextIndex); }
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);//from w w w . j a va 2 s .c om int start = matcher.start(); int end = matcher.end(); System.out.println(start); System.out.println(end); matchFound = matcher.find(); }
From source file:MainClass.java
public static void main(String args[]) { // create regular expression Pattern expression = Pattern.compile("J.*\\d[0-35-9]-\\d\\d-\\d\\d"); String string1 = "Jack's Birthday is 05-12-75\n" + "Joe's Birthday is 11-04-68\n" + "Tom's Birthday is 04-28-73\n" + "Lee" + "s Birthday is 12-17-77"; // match regular expression to string and print matches Matcher matcher = expression.matcher(string1); while (matcher.find()) System.out.println(matcher.group()); }
From source file:ReluctantExample.java
public static void main(String args[]) { String regex = "(\\d+?)"; Pattern pattern = Pattern.compile(regex); String candidate = "1234"; Matcher matcher = pattern.matcher(candidate); while (matcher.find()) { System.out.println(matcher.group()); }// w ww . j av a 2 s .c om System.out.println("Done"); }
From source file:ReplaceExample.java
public static void main(String args[]) { String regex = "(\\w)(\\d)(\\w+)"; Pattern pattern = Pattern.compile(regex); String candidate = "X99SuperJava"; Matcher matcher = pattern.matcher(candidate); String tmp = matcher.replaceAll("$33"); System.out.println("REPLACEMENT: " + tmp); System.out.println("ORIGINAL: " + candidate); }
From source file:MainClass.java
public static void main(String args[]) { String regex = "(\\w)(\\d)(\\w+)"; Pattern pattern = Pattern.compile(regex); String candidate = "W3C"; Matcher matcher = pattern.matcher(candidate); String tmp = matcher.replaceAll("$33"); System.out.println("REPLACEMENT: " + tmp); System.out.println("ORIGINAL: " + candidate); }
From source file:Main.java
public static void main(String args[]) { String regex = "(\\w++)(\\d\\d)(\\w+)"; Pattern pattern = Pattern.compile(regex); String candidate = "X99 test 55"; Matcher matcher = pattern.matcher(candidate); if (matcher.find()) { System.out.println("GROUP 0:" + matcher.group(0)); System.out.println("GROUP 1:" + matcher.group(1)); System.out.println("GROUP 2:" + matcher.group(2)); System.out.println("GROUP 3:" + matcher.group(3)); } else {/*from w ww . ja v a 2 s .c om*/ System.out.println("NO MATCHES"); } }
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 w w w. j a v a2 s. c o m }
From source file:Main.java
public static void main(String[] args) { String s = "this is a good example with 234 songs"; Pattern p = Pattern.compile("this is a (.*?) example with (\\d+) songs"); Matcher m = p.matcher(s);/* w w w . j av a 2s . co m*/ if (m.matches()) { String kind = m.group(1); String nbr = m.group(2); System.out.println("kind: " + kind + " nbr: " + nbr); } }