can specify the number of times a match must occur and whether the match must

Can specify the number of times a match must occur

This preview shows page 105 - 107 out of 517 pages.

can specify the number of times a match must occur and whether the match must be “anchored” to the beginning or end of the string. For example, here is a regex that matches a time of day, such as “8:25pm”, on a line by itself: 1 time_regex = /^\d\d?:\d\d\s*[ap]m$/i This regexp matches a digit at the beginning of a string ( ˆ\d ), optionally followed by another digit ( \d? ), followed by a colon, exactly two digits, zero or more whitespace characters ( \s* ), either a or p , then m at the end of the string ( m$ ) and ignoring case (the i after the closing slash). Another way to match one or two digits would be [0-9][0-9]? and another way to match exactly two digits would be [0-9][0-9] . Ruby allows the use of parentheses in regexes to capture the matched string or substrings. For example, here is the same regexp with three capture groups: 1 x = "8:25 PM" 2 x =~ /(\d\d?):(\d\d)\s*([ap])m$/i The second line attempts to match the string x against the regex. If the match succeeds, the operator will return the position in the string (with 0 being the first character) at which the match succeeded, the global variable $1 will have the value ”8” , $2 will be ”25” , and $3 will be ”P” . The last-match variables are reset the next time you do another regex match. If the match fails, will return nil . Note that nil and false are not actually equal to each other, but both evaluate to “false” when used in a conditional expression (in fact, they are the only two values in Ruby that do so). Idiomatically, methods that are truly Boolean (that is, the only possible return values are “true” or “false”) return false , whereas methods that return an object when successful return nil when they fail. Lastly, note that works on both strings and Regexp objects, so both of the following are legal and equivalent, and you should choose whichever is easiest to understand in the context of your code.
Image of page 105
1 "Catch 22" =~ /\w+\s+\d+/ 2 /\w+\s+\d+/ =~ "Catch 22" Summary A distinguishing primitive type in Ruby is the symbol, an immutable string whose value is itself. Symbols are commonly used in Ruby to denote “specialness,” such as being one of a set of fixed choices like an enumeration. Symbols aren’t the same as strings, but they can easily be converted back and forth with the methods to_s and to_sym . Ruby statements are separated by newlines, or less commonly, by semicolons. Ruby’s regular expression facilities are comparable to those of other modern languages, including support for capture groups using parentheses and for match modifiers such as a trailing i for “ignore case when matching.” Self-Check 3.1.1. Which of the following Ruby expressions are equal to each other: (a) :foo (b) %q{foo} (c) %Q{foo} (d) ’foo’.to_sym (e) :foo.to_s (a) and (d) are equal to each other; (b), (c), and (e) are equal to each other Self-Check 3.1.2. What is captured by $1 when the string 25 to 1 is matched against each of the following regexps: (a) /(\d+)$/ (b) /ˆ\d+([ˆ0-9]+)/ (a) the string “ 1 ” (b) the string “ to ” (including the leading and trailing spaces) Self-Check 3.1.3. When is it correct to write Fixnum num=3
Image of page 106
Image of page 107

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors