List of usage examples for java.util.regex Matcher replaceAll
public String replaceAll(Function<MatchResult, String> replacer)
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) { CharSequence inputStr = "a b c a b c"; String patternStr = "a"; String replacementStr = "x"; Pattern pattern = Pattern.compile(patternStr); // Replace all occurrences of pattern in input Matcher matcher = pattern.matcher(inputStr); String output = matcher.replaceAll(replacementStr); System.out.println(output);/*from w w w . j a va2s . com*/ }
From source file:MainClass.java
public static void main(String args[]) { String str = "Java1 Java2 JDK Java2S Java2s.com"; Pattern pat = Pattern.compile("Java.*? "); Matcher mat = pat.matcher(str); System.out.println("Original sequence: " + str); str = mat.replaceAll("Java "); System.out.println("Modified sequence: " + str); }
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();//ww w . j a va2 s . com 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:BookRank.java
/** Grab the sales rank off the web page and log it. */ public static void main(String[] args) throws Exception { Properties p = new Properties(); String title = p.getProperty("title", "NO TITLE IN PROPERTIES"); // The url must have the "isbn=" at the very end, or otherwise // be amenable to being string-catted to, like the default. String url = p.getProperty("url", "http://test.ing/test.cgi?isbn="); // The 10-digit ISBN for the book. String isbn = p.getProperty("isbn", "0000000000"); // The RE pattern (MUST have ONE capture group for the number) String pattern = p.getProperty("pattern", "Rank: (\\d+)"); // Looking for something like this in the input: // <b>QuickBookShop.web Sales Rank: </b> // 26,252/*from w ww .j ava2 s .c o m*/ // </font><br> Pattern r = Pattern.compile(pattern); // Open the URL and get a Reader from it. BufferedReader is = new BufferedReader(new InputStreamReader(new URL(url + isbn).openStream())); // Read the URL looking for the rank information, as // a single long string, so can match RE across multi-lines. String input = "input from console"; // System.out.println(input); // If found, append to sales data file. Matcher m = r.matcher(input); if (m.find()) { PrintWriter pw = new PrintWriter(new FileWriter(DATA_FILE, true)); String date = // `date +'%m %d %H %M %S %Y'`; new SimpleDateFormat("MM dd hh mm ss yyyy ").format(new Date()); // Paren 1 is the digits (and maybe ','s) that matched; remove comma Matcher noComma = Pattern.compile(",").matcher(m.group(1)); pw.println(date + noComma.replaceAll("")); pw.close(); } else { System.err.println("WARNING: pattern `" + pattern + "' did not match in `" + url + isbn + "'!"); } // Whether current data found or not, draw the graph, using // external plotting program against all historical data. // Could use gnuplot, R, any other math/graph program. // Better yet: use one of the Java plotting APIs. String gnuplot_cmd = "set term png\n" + "set output \"" + GRAPH_FILE + "\"\n" + "set xdata time\n" + "set ylabel \"Book sales rank\"\n" + "set bmargin 3\n" + "set logscale y\n" + "set yrange [1:60000] reverse\n" + "set timefmt \"%m %d %H %M %S %Y\"\n" + "plot \"" + DATA_FILE + "\" using 1:7 title \"" + title + "\" with lines\n"; Process proc = Runtime.getRuntime().exec("/usr/local/bin/gnuplot"); PrintWriter gp = new PrintWriter(proc.getOutputStream()); gp.print(gnuplot_cmd); gp.close(); }
From source file:Main.java
public static CharSequence removeDuplicateWhitespace(CharSequence inputStr) { String patternStr = "\\s+"; String replaceStr = " "; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); return matcher.replaceAll(replaceStr); }
From source file:Main.java
public static String getEnglishNums(String num) { Pattern p = Pattern.compile("(?<=\\d)(?=(\\d\\d\\d)+$)"); Matcher m = p.matcher(num); return m.replaceAll(","); }
From source file:Main.java
public static String subString(String sub) { Pattern pp = Pattern.compile("\\s*|\t|\r|\n"); Matcher mm = pp.matcher(sub); return mm.replaceAll(""); }
From source file:Main.java
public static String strSpecial(String str) { String regEx = "[/\\:*?<>|\"\n\t]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll(""); }
From source file:Main.java
public static String reSpecial(String str) { String regEx = "[/\\:*?<>|\"]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll(""); }