Tuesday, September 3, 2019

Java programming: use Matcher to replace patterns with captured groups (code example)


String str = "=123-abc=   =7890-ABCD=";
Pattern pattern = Pattern.compile("=([0-9]*)-([A-Za-z]*)=");
Matcher matcher = pattern.matcher(str);
if (matcher.find())
{
    str = matcher.replaceAll("# $2 + $1 #");
}

System.out.println(str);


The output will be:
# abc + 123 #   # ABCD + 7890 #

No comments:

 
Get This <