Java regex meta characters example

A regular expression defines a search pattern for strings. This pattern may match one or several times or not at all for a given string. The abbreviation for regular expression is regex. Regular expressions can be used to search, edit and manipulate text.

1. Java regex list of meta characters

Regular expressions support some meta characters or special characters with a definite pre-defined meaning. The following meta characters make certain common pattern easier to use, e.g. 'd' instead of [0..9].

Meta CharacterDescription
dAny digit, short for [0-9]
DA non-digit, short for [^0-9]
sA whitespace character, short for [ tnx0brf]
SA non-whitespace character, for short for [^s]
wA word character, short for [a-zA-Z_0-9]
WA non-word character [^w]
S+Several non-whitespace characters
bMatches a word boundary. A word character is [a-zA-Z0-9_] and b matches its bounderies.

1. Java regex meta characters example

package com.howtodoinjava.regex;

import junit.framework.Assert;

import org.junit.Test;

public class MetaCharactersTest
{
	@Test
	public void testAnyDigit()
	{
		/**
		 * Matches exactly 3 digits
		 * */
		String sampleText = "123";
		String regex = "d{3}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonDigit()
	{
		/**
		 * Matches exactly 3 non digits
		 * */
		String sampleText = "abc";
		String regex = "d{3}";
		Assert.assertNotSame(true, sampleText.matches(regex));
	}

	@Test
	public void testWhiteSpaceCharacters()
	{
		/**
		 * Matches n number of spaces
		 * */
		String sampleText = "     ";
		String regex = "s{0,}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonWhiteSpaceCharacter1()
	{
		/**
		 * Matches no space allowed
		 * */
		String sampleText = "abc123";
		String regex = "S{0,}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonWhiteSpaceCharacter2()
	{
		/**
		 * Matches no space allowed with '+' sign
		 * */
		String sampleText = "abc123";
		String regex = "S+";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testWordCharacter()
	{
		/**
		 * Matches one or more word characters
		 * */
		String sampleText = "abc123";
		String regex = "w{0,}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonWordCharacter()
	{
		/**
		 * Matches one or more word characters
		 * */
		String sampleText1 = "abc123";
		String sampleText2 = "!@#$";
		String regex = "W{0,}";
		Assert.assertNotSame(true, sampleText1.matches(regex));
		Assert.assertEquals(true, sampleText2.matches(regex));
	}

	@Test
	public void testWordBoundary()
	{
		/**
		 * Matches the word "abc123" exactly same
		 * */
		String sampleText = "abc123";
		String regex = "babc123b";
		Assert.assertEquals(true, sampleText.matches(regex));
	}
}

In this Java regex example, we learned to use meta characters in regular expressions to evaluate text strings.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.