Java Regex pattern.matcher Understanding -
consider following code
import java.util.regex.*; public static void main(string[] args) { string str = "suneetha n.=9876543210, pratish patil=9898989898"; pattern pattern = pattern.compile("(\\w+)(\\s\\w+)(=)(\\d{10})"); matcher matcher = pattern.matcher(str); string newstr = matcher.replaceall("$4:$2,$1"); system.out.println(newstr); }
output of above code is
suneetha n.=9876543210, 9898989898: patil,pratish
i not able understand use of matcher.replaceall("$4:$3,$1")
, how works , produces output. please provide suggestion on it.
you have
"(\\w+)(\\s\\w+)(=)(\\d{10})"
regex , imagine create groups founded string. in example
pratish patil=9898989898
and here groups regex:
(\\w+) => pratish $1 (\\s\\w+) => patil $2 (=) => = $3 (\\d{10}) => 9898989898 $4
then said want replaceall
regex new ordering $number defined group. replacing
pratish patil=9898989898
by new group order :
, ,
.
$4:$2,$1 -> 9898989898:patil,pratish.
you didnt use $3 group, =
.
Comments
Post a Comment