[oak perl] Backslash Space in Regex?

Elijah Griffin oaklandpm at eli.users.panix.com
Wed Jul 5 15:35:32 PDT 2006


Eugene wrote:
> In the regex part of the introduction,
> the author has an example in which the regex contains both
> * \s 
> and 
> * backslash space
> In his overall explanation of the regex,
> he doesn't distinguish between the two.

Backslash space (\ ) is a literal space. Backslash s (\s) is any of
a number of white space characters. Most of the time you will not
need backslash space because a space without a backslash is normally
a literal space. The only time you need the backslash is if you are
using the /x option for the regex. When /x is in effect whitespace
(that is not backslash escaped) is ignored in a regex.

Compare:

	/^((Jan|Mar|May|Jul|Aug|Oct|Dec) ([ 012]\d|3[01])|(Apr|Jun|Sep|Nov) ([ 012]\d|30)|Feb ([ 12]\d)) (19\d\d|20\d\d)$/

With:
	/^ (
             (Jan|Mar|May|Jul|Aug|Oct|Dec)\ ([ 012]\d|3[01])
           | (Apr|Jun|Sep|Nov)\ ([ 012]\d|30)
           |  Feb\ ([ 012]\d)
           )\ (19\d\d|20\d\d)
         $/x

Both require a space after the month three-letter-name, and then require
a two character (zero or space padded) date, then require another space
and the year.

The second one is arranged to make it much easier to read, thanks to the
/x option. But to match a literal space with /x, the space must either be
in [square backets] or backslash escaped. I used both methods in this
example.

If I were to use backslash spaces in the first, it would not change the
effect of that regex.

Elijah



More information about the Oakland mailing list