05. Matching multiple characters

With an Asterisk

The asterisk (*) allows you to match zero or a multiple number of the preceding character.

$ ls /usr/bin | grep 'bc*t'
btmmdiagnose ibtool libtool pfbtops

In this example, we are matching commands within our bin folder that have a b, any number of c's (including none), then a t.

Although convenient, the asterisk may be too powerful for your purposes. To have more control over the number of repeating elements, we can use braces.

Using Braces to get more specific

With braces ({...}), we are able to better control how many times an element occurs. In BRE, remember that we must escape the brace characters with a backslash.

Match exactly n occurrences of the preceding regex.
Match at least n occurrences of the preceding regex.
Match between n and m occurrences of the preceding regex.
$ ls /usr/bin | grep '[[:alpha:]]{2}'
perlthanks5.16 perlthanks5.18 piconv5.16 piconv5.18 pl2pm5.16 pl2pm5.18 pod2html5.16 pod2html5.18 pod2latex5.16 pod2latex5.18 pod2man5.16 pod2man5.18 pod2readme5.16 pod2readme5.18

The above code would search for lines of text that have a two digit number.


Note that the values for n and m must be between 0 and RE_DUP_MAX. This variable signifies the largest number of repetitions you are allowed in regular expressions.

To check your system's setting for this value, use the command getconf.

$ getconf RE_DUP_MAX

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources