# We use !! to convert the return value to a boolean # like 999.999.999.999, but in this case we just care about the format. We can combine everything we learned so far to create more complex regular expressions.Įxample: Does this look like an IP address? # Note that this will also match some invalid IP address To match multiple characters we can use pattern modifiers. Up until now we have only been able to match a single character at a time. # In this case only the literal dot matches then you will have to escape it.Įxample: Escaping special characters # If we don't escape, the letter will match There is also the negative form of these: \s matches white space (tabs, regular space, newline).There is a nice shorthand syntax for specifying character ranges: Remember: the return value when using `=~` is either the string index or `nil` matches any letter from a to z (no caps)Įxample: Does this string contain any numbers? def contains_number(str)Ĭontains_number("The year is 2015") # returns 12Ĭontains_number("The cat is black") # returns nil.In other words, a range like is the same as. We can use ranges to match multiple letters or numbers without having to type them all out. This will not take into account the amount of characters, we will see how to do that soon. For example, matches any vowel.Įxample: Does the string contain a vowel? def contains_vowel(str) Character ClassesĪ character class lets you define a range or a list of characters to match. You are going to learn how to build more advanced patterns so you can match, capture
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |