A Few More Patterns

To finish up—because we have a little space left over today—I'd like to tuck a couple more patterns into your regular expressions repertoire, so that you can keep building on what you've learned (and so that I can use them in the last example you're about to work through).

So far you've learned about patterns with individual characters:

/abc/

Patterns with digits:

/d/

And patterns with nondigits:

/D/

Here's another one: s is a whitespace character. Whitespace in Perl is a space, a tab, a newline, a carriage return, or a formfeed. The s pattern counts as any of these. Just as with d, however, s only means a single whitespace character, so the pattern /s/ will match one and only one space, tab, newline, and so on. Grouping all the whitespace characters together under one special character allows you to not worry about whether your user is typing spaces or tabs, or whether the system your running on is using newlines or carriage returns or both. It's just whitespace. The general rule is if its whitespace, use s in your pattern.

S is the opposite of s. Its any character that isn't whitespace: any number or letter or punctuation; anything that isn't a space, a tab, a newline, and so on.

The problem with the d, D, s and S characters, as I've mentioned, is that they match only a single character. Sometimes it would be useful to be able to match one or more of these characters, so your pattern would be true if you had one digit or eight, or one space or four. Either way would work. The pattern would be much more flexible that way.

You can do that by adding a + to the pattern. The + applies to the character just before it. So this pattern matches one or more digits:

/d+/

This pattern matches one or more whitespace characters:

/s+/

But this pattern matches a single whitespace character, followed by one or more digits:

/sd+/

The + only applies to the pattern just before it. It can also apply to any pattern, not just the s or d special patterns. This pattern, for example, matches one or more m's:

/m+/

Table 5.1 shows our pattern summary so far.

Table 5.1. Patterns
Pattern Type What it does
/a/ Character Matches a
/d/ Any digit Matches a single digit
/D/ Any character not a digit Matches a single character other than a digit
/s/ Any whitespace Matches a single space, tab, newline, carriage return or formfeed
/S/ Any character not whitespace Matches a single character other than whitespace
+ One or more Matches one or more of the pattern just before it

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset