Ruby/Development/Regexps
Материал из Wiki.crossplatform.ru
An example of a regular expression used to match a string with the String method scan.
hamlet = "The slings and arrows of outrageous fortune" puts hamlet.scan(/\w+/)
grep(/\Aen in/)
opening = "man men woman women" p opening.grep(/\Aen in/)
grep(/colou?r/)
color = "color colour" color.grep(/colou?r/) # => ["I think that colour is just right for you office."]
grep(/[\(\d{3}\)]?\d{3}-\d{4}/)
phone = "(555)123-4567" phone.grep(/[\(\d{3}\)]?\d{3}-\d{4}/)# => ["(555)123-4567"]
grep(/[\(\d+\)]?\d+-\d+/)
phone = "(555)123-4567" phone.grep(/[\(\d+\)]?\d+-\d+/) # => ["(555)123-4567"]
grep(/[\(\d\d\d\)]?\d\d\d-\d\d\d\d/)
phone = "(555)123-4567" phone.grep(/[\(\d\d\d\)]?\d\d\d-\d\d\d\d/) # => ["(555)123-4567"]
grep(/e n,\z/)
opening = "man men woman women" p opening.grep(/e n,\z/)
grep(/m[ae]n/)
opening = "man men woman women" p opening.grep(/m[ae]n/)
grep(/m(e|a)n/)
opening = "man men woman women" p opening.grep(/m(e|a)n/)
grep(/men/)
opening = "this is a test \n this is a test,\n" p opening.grep(/test/)
grep(/men|man/)
opening = "man men woman women" p opening.grep(/men|man/)
grep(/outcast state,$/)
opening = "man men woman women" p opening.grep(/m a,$/)
grep(/^When in/)
opening = "man men woman women" p opening.grep(/^en in/)
Implementing Class and Singleton Methods
class Regexp def Regexp.is_valid?(str) begin compile(str) valid = true rescue RegexpError valid = false end end end Regexp.is_valid? "The horror!" # => true Regexp.is_valid? "The)horror!" # => false
Looking for a word
line = "A horse! a horse! my kingdom for a horse!" line[/horse!$/] # => "horse!"
Match a regular repression in if statement
line = "abc" if line =~ /Perl|Python/ puts "Scripting language mentioned: #{line}" end
Match a time
/\d\d:\d\d:\d\d/ # a time such as 12:34:56
MatchData-related $-variables are in $~
re = /(\d+):(\d+)/ md1 = re.match("Time: 12:34am") md2 = re.match("Time: 10:30pm") p [ $1, $2 ] $~ =md1 p [ $1, $2 ]
Match Perl, a space, and Python
/Perl Python/ # Perl, a space, and Python
Match Perl, one or more spaces, and Python
/Perl +Python/ # Perl, one or more spaces, and Python
Match Perl, whitespace characters, then Python
/Perl\s+Python/ # Perl, whitespace characters, then Python
Match Perl, zero or more other chars, then Python
/Perl.*Python/ # Perl, zero or more other chars, then Python
Match Perl, zero or more spaces, and Python
/Perl *Python/ # Perl, zero or more spaces, and Python
Match Ruby, a space, and either Perl or Python
/Ruby (Perl|Python)/ # Ruby, a space, and either Perl or Python
Regexps use elements to instruct the regular expression engine on how to find a given string.
A combination of the special characters, enclosed by a pair of slashes (//), makes up a regular expression pattern. ^ Matches the beginning of a line $ Matches the end of a line \w Matches a word character [...] Matches any character in the brackets [^...] Matches any characters not in the brackets * Matches zero or more occurrences of the previous regexp + Matches one or more occurrences of the previous regexp ? Matches zero or one occurrences of the previous regexp
Replace Perl or Python with Ruby
line = "Perl or Python" puts line.gsub(/Perl|Python/, "Ruby")
Search for a word from the very beginning
line = "A horse! a horse! my kingdom for a horse!" line[/^A horse/, 0] # => "A horse"
sub and gsub replace with regular expressions
line = "abc" line.sub(/Perl/, "Ruby") # replace first "Perl" with "Ruby" line.gsub(/Python/, "Ruby") # replace every "Python" with "Ruby"
The method Regexp#match matches a regular expression against a string.
# If unsuccessful,the method returns nil. # On success,it returns an instance of class MatchData. # And that MatchData object gives you access to all available in formation. re = /cat/ puts re.class # Regexp re = /(\d+):(\d+)/ # match a time hh:mm md = re.match("Time: 12:34am") puts md.class puts md[0] puts md[1] puts md[2] puts md.pre_match puts md.post_match