Example of the appendReplacement and appendTail methods

Let's look at a complete program to the understand use of these methods.

Consider the following input:

<n1=v1 n2=v2 n3=v3> n1=v1 n2=v2 abc=123 <v=pq id=abc> v=pq 

We need to write code to swap each name-value pair enclosed in angular brackets, < and >, while leaving the name-value pairs outside the angular brackets unchanged. After running our code, it should produce the following output:

<v1=n1 v2=n2 v3=n3> n1=v1 n2=v2 abc=123 <pq=v abc=id> v=pq 

To solve this problem, we have to first find each match enclosed in angular brackets using the find method in a loop. Inside the loop, we will have to replace each name-value pair using the appendReplacement method. Finally, outside the loop, ...

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.