红宝石 |正则表达式 compile()函数
Regexp#compile() : compile()是一个 Regexp 类方法,它完成了一个新的正则表达式。
Syntax: Regexp.compile()
Parameter: Regexp values
Return: creates a new Regular Expression.
示例 #1:
# Ruby code for Regexp.compile() method
# declaring Regexp value
reg_a = Regexp.compile(/a/)
# declaring Regexp value
reg_c = Regexp.compile('\*?{}.')
# compile method
puts "Regexp compile form : #{reg_a}\n\n"
puts "Regexp compile form : #{reg_c}\n\n"
输出 :
Regexp compile form : (?-mix:a)
Regexp compile form : (?-mix:\*?{}.)
示例 #2:
# Ruby code for Regexp.compile() method
# declaring Regexp value
reg_a = Regexp.compile(/geeks/)
# declaring Regexp value
reg_b = Regexp.compile('/(?.)(?.)(?.)/')
# declaring Regexp value
reg_c = Regexp.compile('\*?????{}.')
# compile method
puts "Regexp compile form : #{reg_a}\n\n"
puts "Regexp compile form : #{reg_b}\n\n"
puts "Regexp compile form : #{reg_c}\n\n"
Regexp compile form : (?-mix:geeks)
Regexp compile form : (?-mix:\/(?.)(?.)(?.)\/)
Regexp compile form : (?-mix:\*?????{}.)