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*

2 thoughts on “Java Regular Expression to mask information from a Text”

  1. Hi, Neat post. There’s an issue along with your website in web explorer, could test
    this? IE still is the market chief and a good portion of other people will pass over your fantastic writing
    due to this problem.

  2. 126454 458494There a few interesting points over time here but I dont know if I see them all center to heart. There exists some validity but Let me take hold opinion until I look into it further. Quite excellent post , thanks and now we want far more! Included with FeedBurner at the same time 843924

Leave a Reply

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