Lesson 2: Matching Single Characters
Lesson 2: Matching Single Characters
In this lesson you’ll learn how to perform simple character matches of one or more characters.
Text
RegEx
Ben
Result
Analysis
The regular expression used here is literal text and it matches Ben in the original text.
Note
In the examples you’ll see that the matched text is shaded. We’ll use this format throughout the book so you can easily
see exactly what an example matched.
Let’s look at another example using the same search text and a different regular expression:
Text
RegEx
my
Result
Click here to view code image
Hello, my name is Ben. Please visit
my website at http://www.forta.com/.
Analysis
my is also static text, but notice how two occurrences of my were matched.
Here is an example:
Text
RegEx
sales.
Result
Analysis
Here the regex sales. is being used to find all filenames starting with sales and followed by another character.
Three of the nine files match the pattern.
Tip
You’ll often see the term pattern used to describe the actual regular expression.
Note
Notice that regular expressions match patterns with string contents. Matches will not always be entire strings, but the
characters that match a pattern—even if they are only part of a string. In the example used here, the regular
expression did not match a filename; rather, it matched part of a filename. This distinction is important to remember
when passing the results of a regular expression to some other code or application for processing.
. matches any character, alphabetic characters, digits, and even . itself:
Text
RegEx
sales.
Result
Analysis
This example contains one additional file, sales.xls. The file was matched by the pattern sales. as . matches
any character.
Multiple instances of . may be used, either together (one after the other—using .. will match any two characters
next to each other) or in different locations in the pattern.
Let’s look at another example using the same text. This time you need to find all files for North America (na) or South
America (sa) regardless of what digit comes next:
Text
RegEx
.a.
Result
Text
RegEx
.a..
Result
Analysis
.a.. does not work any better than .a. did; appending a . will match any additional character (regardless of what
it is). How then can you search for . when .is a special character that matches any character?
Text
Click here to view code image
sales1.xls
orders3.xls
sales2.xls
sales3.xls
apac1.xls
europe2.xls
na1.xls
na2.xls
sa1.xls
RegEx
.a.\.
Result
Analysis
.a.\. did the trick. The first . matched n (in the first two matches) or s (in the third). The second . matched 1 (in
the first and third matches) or 2 (in the second). \. then matched the . separating the filename from the extension.
The example could be further improved by including the xls in the pattern so as to prevent a filename such
as sa3.doc from being matched, like this:
Text
.a.\.xls
Result
SUMMARY
Regular expressions, also called patterns, are strings made up of characters. These characters may be literal (actual
text) or metacharacters (special characters with special meanings), and in this lesson you learned how to match a
single character using both literal text and metacharacters. . matches any character. \ is used to escape characters
and to start special character sequences.