02. Matching Characters and Digits

One digit

Match any digit from 0 to 9 using \d.

To match any non-digit, use the upper-case \D.

One character

Use the dot (.). This will match any character - a letter, digit or whitespace. To use the actual period scape with a backslash \..

One alphanumeric character

Any alphanumeric character including the underscore. Use \w. As you'll learn soon, this is the same as [a-zA-Z0-9_].

Any non-alphanumeric character is \W. This is helpful especially for matching punctuations.

Boundary between word and non-word

\b matches the boundary between a word and a non-word character.

Matching one from set of characters

Place each character within square brackets.

[abc]
Match a, b or c.

Excluding matches to set of specific characters

Use the square brackets, just like above, with a hat (^) inside of it.

[^abc]
Match any character except for a, b or c.

Range between two letters or two digits

There is a shortcut to specifying any range of letters or digits. Instead of writing [abcdefghijklmnopqrstuvwxyz] we can just write write the range between two letter or numbers, separated by a dash (-).

[0-4]
Matches digits from 0 to 4.
[a-z]
All lowercase letters from a to z.
[A-Z]
All uppercase letters from A to Z.

Whitespace

The whitespace special character \s will match single type of whitespace - a space, tab, newline or carriage return.

To specify any number of whitespace character use the \s*.

Any non-whitespace character will be \S.

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Build modern and responsive webpages

High Performance Web Sites

Build modern and responsive webpages Try Front-end

Want your web site to display more quickly? This book presents 14 specific rules that will cut 25% to 50% off response time when users request a page. Author Steve Souders, in his job as Chief Performance Yahoo!, collected these best practices while optimizing some of the most-visited pages on the Web.

$ Check price
29.9929.99Amazon 4.5 logo(63+ reviews)

More Front-end resources

Ad