Java code
Public static void main (String [] args ){
String regex = "\\\ \\{ (. + ?) \\}";
String str = "<a onclick = \" showUserName ('$ {session. user. username} ',' $ {session. user. password} '); \ "> linkme </a> ";
Pattern pattern = Pattern. compile (regex );
System. out. println (pattern );
Matcher matcher = pattern. matcher (str );
StringBuffer sb = new StringBuffer ();
While (matcher. find ()){
System. out. println (">>>> replace sequence:" + matcher. group (0 ));
System. out. println (">>>> index range: (" + matcher. start () + "," + matcher. end () + ")");
System. out. println (">>>> sub:" + str. substring (matcher. start (), matcher. end ()));
Matcher. appendReplacement (sb, "123 ");
System. out. println ("-----------------");
}
Matcher. appendTail (sb );
System. out. println (">>> sb:" + sb. toString ());
}
Output:
\ $ \ {(. + ?) \}
>>>>> Replace sequence :$ {session. user. username}
>>>>> Index range: (26, 50)
>>>>> Sub :$ {session. user. username}
-----------------
>>>>> Replace sequence :$ {session. user. password}
>>>>> Index range: (53, 77)
>>>>> Sub :$ {session. user. password}
-----------------
>>>> Sb: <a onclick = "showUserName ('000000', '000000');"> linkme </a>
Author: "penggle"