List of usage examples for java.util.regex Pattern compile
public static Pattern compile(String regex)
From source file:MainClass.java
public static void main(String args[]) { String candidateString = "My name is Bond. James Bond."; String matchHelper[] = { " ^", " ^" }; Pattern p = Pattern.compile("Bond"); Matcher matcher = p.matcher(candidateString); // Find the starting point of the first 'Bond' matcher.find();//from w w w .j a v a 2s. co m int startIndex = matcher.start(); System.out.println(candidateString); System.out.println(matchHelper[0] + startIndex); }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "a b"; String patternStr = "a b"; // Compile without comments Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.matches(); // Compile with comments pattern = Pattern.compile(patternStr, Pattern.COMMENTS); matcher = pattern.matcher(inputStr); matchFound = matcher.matches(); // false // Use an inline modifier matchFound = pattern.matches("(?x)a \\s b", inputStr); }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "a b"; String patternStr = "a b"; // Compile without comments Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.matches(); // Compile with comments pattern = Pattern.compile(patternStr, Pattern.COMMENTS); matcher = pattern.matcher(inputStr); matchFound = matcher.matches(); // false // Use an inline modifier matchFound = pattern.matches("a (?x: b )", inputStr); }
From source file:MainClass.java
public static void main(String args[]) { Pattern pat;// w w w.ja v a 2s . c o m Matcher mat; 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:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 true "; Scanner scanner = new Scanner(s); // skip the word that matches the pattern .com scanner.skip(Pattern.compile(".com")); System.out.println(scanner.nextLine()); scanner.close();// w w w . ja v a2 s.c om }
From source file:Main.java
public static void main(String[] args) { // A group of 3 digits followed by 7 digits. String regex = "\\b(\\d{3})\\d{7}\\b"; // Compile the regular expression Pattern p = Pattern.compile(regex); String source = "12345678, 12345, and 9876543210"; // Get the Matcher object Matcher m = p.matcher(source); // Start matching and display the found area codes while (m.find()) { String phone = m.group(); String areaCode = m.group(1); System.out.println("Phone: " + phone + ", Area Code: " + areaCode); }//from w ww . j a v a 2 s . c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { String inputStr = "abbabcd"; String patternStr = "(a(?:b*))+(c*)"; // (?:b*) is a non-capturing group Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); if (matchFound) { for (int i = 0; i <= matcher.groupCount(); i++) { String groupStr = matcher.group(i); }/*from w w w. j a v a 2 s . co m*/ } }
From source file:FindA.java
public static void main(String args[]) throws Exception { String candidate = "A Matcher examines the results of applying a pattern."; String regex = "\\ba\\w*\\b"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(candidate); String val = null; System.out.println("INPUT: " + candidate); System.out.println("REGEX: " + regex + "\r\n"); while (m.find()) { val = m.group(); System.out.println("MATCH: " + val); }/*from www . j ava2 s . c om*/ if (val == null) { System.out.println("NO MATCHES: "); } }
From source file:Resetting.java
public static void main(String[] args) throws Exception { Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags"); while (m.find()) System.out.println(m.group()); m.reset("fix the rig with rags"); while (m.find()) System.out.println(m.group()); }
From source file:Main.java
public static void main(String[] args) { String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<postPhoneNumber>\\d{4})\\b"; String source = "1234567890, 12345, and 9876543210"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(source); while (m.find()) { String matchedText = m.group(); int start1 = m.start("areaCode"); int start2 = m.start("prefix"); int start3 = m.start("postPhoneNumber"); System.out.println("Matched Text:" + matchedText); System.out.println("Area code start:" + start1); System.out.println("Prefix start:" + start2); System.out.println("Line Number start:" + start3); }/*from www. j a v a 2 s . c om*/ }