Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Repeat: Sequences of Characters > ? Zero or One Time

? Zero or One Time

To find, say, both British and American spellings harbour of harbor, search for harbou?r. The scope of ? is just one character, so in this example it applies only to the u. The expression paraphrases as “find harbo, perhaps followed by u (or, followed by zero or one occurrence of u), followed by r.” To extend the scope of ? to more characters, group them with parentheses: (19)?\d\d finds all two-digit numbers (matched by \d\d), as well as four-digit numbers whose first two digits are 19. You can combine this with alternation: (18|19|20)?\d\d matches two-digit numbers as well as four-digit numbers that begin with 18, 19, or 20.

The ? operator is useful for matching numbers as well—for example, in dates. The expression \d\d-\d\d-\d\d\d\d finds dates formatted like 08-04-2007. But dates come in different shapes, so to allow for single-digit days and months and double-digit years, use \d\d?-\d\d?-(\d\d)?\d\d.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint