To generate a replacement sequence of ab, the
To generate a replacement sequence of ab, the String literal argument to replaceAll() must be a\\b (see Example 5-7). Be careful when counting those backslashes! Example 5-7. Backslashes in regular expressions package com.ronsoft.books.nio.regex; import java.util.regex.Pattern; import java.util.regex.Matcher; /** * Demonstrate behavior of backslashes in regex patterns. * * @author Ron Hitchens (ron@ronsoft.com) */ public class BackSlashes{ public static void main (String [] argv) { // Substitute “ab” for XYZ or ABC in input String rep = “a\\b”; String input = “> XYZ <=> ABC <"; Pattern pattern = Pattern.compile ("ABC|XYZ"); Matcher matcher = pattern.matcher (input); System.out.println (matcher.replaceFirst (rep)); System.out.println (matcher.replaceAll (rep)); // Change all newlines in input to escaped, DOS-like CR/LF rep = "\\r\\n"; input = "line 1nline 2nline 3n"; pattern = Pattern.compile ("\n"); matcher = pattern.matcher (input); System.out.println (""); System.out.println ("Before:"); System.out.println (input); System.out.println ("After (dos-ified, escaped):"); System.out.println (matcher.replaceAll (rep)); } } Here's the output from running BackSlashes: > ab <=> ABC < > ab <=> ab < Before: line 1 line 2 line 3 After (dos-ified, escaped): line 1rnline 2rnline 3rn 182
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services