The point of this exercise may not be obvious, but in the real world,
you'll often have to do something similar. Someday,
you'll be unlucky enough to have a confusing program to
maintain, and you'll wonder what the author was trying to
accomplish.[394]
/"([^"]*)"/ matches a simple string in double
quotes. By a "simple" string, we don't mean one
like Perl's double-quoted strings, which could contain a
backslashed double-quote mark or other backslash magic. This matches
just a double-quote mark, the contents of the string (which
can't contain a double quote), and a closing quote mark. The
contents may be empty. The parentheses aren't needed for
grouping, so they seem to be memory parentheses; as we'll see
in the next chapter, this regular expression memory, which holds the
quoted substring, is probably being saved for some later use. Perhaps
this pattern would be used in reading a configuration file with
quoted strings, although in that case it should probably use anchors.
/^0?[0-3]?[0-7]{1,2}$/ matches if the string has
nothing but an octal number (perhaps with a leading zero) in the
range from 0 through 0377. Note
that this one is anchored at both ends, so it doesn't allow
anything else in the string before or after the number. (The previous
pattern wasn't anchored; it could match anywhere in the
string.)
/^\b[\w.]{1,12}\b$/ matches strings made up of
nothing but letters, digits, underscores, and dots, but never
starting or ending with a dot. Also, the strings are limited to a
maximum of 12 characters.
You may have noticed that the dot inside the character class is not
special, so it doesn't need to be backslashed. That makes the
character class match ordinary letters, digits, and underscores, and
also dots.
The way we can be sure that this one won't allow a string to
start or end with a dot is that it has both a word-boundary anchor
and a start-of-string or end-of-string anchor at each end of the
string. The word-boundary anchor can match only if there's a
"word" starting or ending there, and a dot can't be
part of a word.