Consider the following program and predict the output:
import java.util.regex.*; public class Main { public static void main(String[] args) { String str =/*from w w w .j av a2 s. c om*/ "Java N.=1234567890, SQL Javascript=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); } }
c)
The first contact does not match with the specified regex (since "." is not covered by "\w+");
hence, the first part of the string is unchanged.
The second part of string matches with the specified regex, so the replace rearranges the substring.