GroupInRep

// Compile regular expression
String patternStr = "\((\w+)\)";
String replaceStr = "<$1>";
Pattern pattern = Pattern.compile(patternStr);

// Replace all (w+) with <$1>
CharSequence inputStr = "a (b c) d (ef) g";
Matcher matcher = pattern.matcher(inputStr);
String output = matcher.replaceAll(replaceStr);
// a (b c) d <ef> g

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.