Java Regular Expression to mask information from a Text

This tutorial shows you how to mask information from a Text using Regular expression.

Related articles:
Java Regular Expression Overview – Syntax
Java Regular Expression to extract data from a Text

I. Problem

Assume that we already have a string that contains User data like this:

What we need is to mask sensitive users’ information using Regular Expression.

II. Practice

We have to make our own pattern to find and replace it from input text.
There are many ways to do this, for more details for how to choose syntax for the string pattern, please read this article: Java Regular Expression Overview – Syntax.
In this case, we can create a pattern like this:

String pattern = "(userID: )(\\d+)(.+Phone Number: )(\\d+-\\d+-\\d+)";

You can see that we have 4 groups, the data we will mask are only in the 2nd group and 4th group.
So the Java code should be similar to:

		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(txt);

		while (m.find()) {
			//replace 2nd group with *userID*
			//replace 4th group with *phoneNumber*

To do that thing, we use 2 methods:

// Implements a non-terminal append-and-replace step.
public Matcher appendReplacement(StringBuffer sb, String replacement);
// Implements a terminal append-and-replace step.
public StringBuffer appendTail(StringBuffer sb);
III. Source code
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainApp {

	public static void main(String[] args) {

		String txt = "Username: jackson12, userID: 201708021234, Phone Number: 083-645-1234"
				+ "\nUsername: aladin_us, userID: 201708026789, Phone Number: 099-699-6789";

		String pattern = "(userID: )(\\d+)(.+Phone Number: )(\\d+-\\d+-\\d+)";

		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(txt);

		StringBuffer sb = new StringBuffer();
		while (m.find()) {

			System.out.println("found userID >> " +;
			System.out.println("found Phone Number >> " +;

			m.appendReplacement(sb, + "*userID*" + + "*phoneNumber*");

Run the code, the result shows in console window:

found userID >> 201708021234
found Phone Number >> 083-645-1234
found userID >> 201708026789
found Phone Number >> 099-699-6789
Username: jackson12, userID: *userID*, Phone Number: *phoneNumber*
Username: aladin_us, userID: *userID*, Phone Number: *phoneNumber*

By grokonez | February 8, 2017.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *