O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required