The number of capture groups in the regular
{ public static void main (String [] argv) { // sanity check, need at least three args if (argv.length < 3) { System.out.println ("usage: regex replacement input ..."); return; } // Save the regex and replacment strings with mnemonic names String regex = argv [0]; String replace = argv [1]; // Compile the expression; needs to be done only once Pattern pattern = Pattern.compile (regex); // Get a Matcher instance and use a dummy input string for now Matcher matcher = pattern.matcher (""); // print out for reference System.out.println (" regex: '" + regex + "'"); System.out.println (" replacement: '" + replace + "'"); // For each remaining arg string, apply the regex/replacmentfor (int i = 2; i < argv.length; i++) { System.out.println ("------------------------"); matcher.reset (argv [i]); System.out.println (" input: '" + argv [i] + "'"); System.out.println ("replaceFirst(): '" + matcher.replaceFirst (replace) + "'"); System.out.println (" replaceAll(): '" + matcher.replaceAll (replace) + "'"); } } } And here's the output from running RegexReplace: regex: '([bB])yte' replacement: '$1ite' input: 'Bytes is bytes' replaceFirst(): 'Bites is bytes'replaceAll(): 'Bites is bites' Remember that regular expressions interpret backslashes in the strings you provide. Also remember that the Java compiler expects two backslashes for each one in a literal String. This means that if you want to escape a backslash in the regex, you'll need two backslashes in the compiled String. To get two backslashes in a row in the compiled regex string, you'll need four backslashes in a row in the Java source code. 181
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services