CHAPTER 10 BATTERIES (Make web site) INCLUDED 237 SPECIAL CHARACTERS
Tuesday, June 10th, 2008CHAPTER 10 BATTERIES INCLUDED 237 SPECIAL CHARACTERS IN CHARACTER SETS In general, special characters such as dots, asterisks, and question marks have to be escaped with a backslash if you want them to appear as literal characters in the pattern, rather than function as regexp operators. Inside character sets, escaping these characters is generally not necessary (although perfectly legal). You should, however, keep in mind the following rules: You do have to escape the caret (^) if it appears at the beginning of the character set unless you want it to function as a negation operator. (In other words, don t place it at the beginning unless you mean it.) Similarly, the right bracket (]) and the dash (-) must be put either at the beginning of the character set or escaped with a backslash. (Actually, the dash may also be put at the end, if you wish.) Alternatives and Subpatterns Character sets are nice when you let each letter vary independently, but what if you want to match only the strings ‘python’ and ‘perl’? You can t specify such a specific pattern with character sets or wildcards. Instead, you use the special character for alternatives: the pipe character (|). So, your pattern would be ‘python|perl’. However, sometimes you don t want to use the choice operator on the entire pattern just a part of it. To do that, you enclose the part, or subpattern, in parentheses. The previous example could be rewritten as ‘p(ython|erl)’. (Note that the term subpattern can also be used about a single character.) Optional and Repeated Subpatterns By adding a question mark after a subpattern, you make it optional. It may appear in the matched string, but it isn t strictly required. So, for example, the (slightly unreadable) pattern r’(http://)?(www.)?python.org’ would match all of the following strings (and nothing else): ‘http://www.python.org’ ‘http://python.org’ ‘www.python.org’ ‘python.org’ A few things are worth noting here: I ve escaped the dots, to prevent them from functioning as wildcards. I ve used a raw string to reduce the number of backslashes needed. Each optional subpattern is enclosed in parentheses. The optional subpatterns may appear or not, independently of each other.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.