The dot in regular expressions

One of the most important characters in regular expressions is the simple dot (strictly the full stop character). The dot essentially matches any character. For example, the following expression means "a digit plus any other character":

[0-9].

In fact, the dot is often most useful when combined with another special character– the asterisk– which we'll look at properly shortly. But, jumping the gun a little, the sequence .* means "zero or more instances of any character". We used this sequence in our introduction to regular expressions when we used the following expression to mean "contains a sequence of ten or more digits":

.*[0-9]{10}.*

Really, this expression means "any number (including zero) of any character, followed by twn digits, followed by any number of any character".

The asterisk is an example of a repetition operator, which we look at on the next page.


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.