Thursday, July 11, 2013

Java regular expression group and backreference

String EXAMPLE_TEST = "<TITLE ...>abc</TITLE>";
String pattern = "(?i)(<title.*?>)(.+?)(</title>)";
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "$1"));
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "$2"));
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "$3"));
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "$1<B>$2</B>$3"));

The result will be

<TITLE ...>
abc
</TITLE>
<TITLE ...><B>abc</B></TITLE>


(?i) at the beginning of the regular expression is to make it case insensitive.



Brackets in regular expression separate it to different group. Via the $ you can refer to a group. $1 is the first group, $2 the second, etc



In the example above, $1 is <TITLE ...> , $2 is abc, $3 is </TITLE>

No comments:

Post a Comment