红宝石 |正则表达式 to_s()函数
Regexp#to_s() : to_s()是一个 Regexp 类方法,它返回包含具有相同语义的正则表达式的字符串。
Syntax: Regexp.to_s()
Parameter: Regexp values
Return: string containing the regular expression with the same semantics.
示例 #1:
# Ruby code for Regexp.to_s() method
# declaring Regexp value
reg_a = /a/
# declaring Regexp value
reg_b = /\xa1\xa2/e
# to_s method
puts "Regexp to_s form : #{reg_a.to_s}\n\n"
puts "Regexp to_s form : #{reg_b.to_s}\n\n"
输出 :
Regexp to_s form : (?-mix:a)
Regexp to_s form : (?-mix:\xa1\xa2)
示例 #2:
# Ruby code for Regexp.to_s() method
# declaring Regexp value
reg_a = /geeks/ix
# declaring Regexp value
reg_b = /(.)(.)/
# to_s method
puts "Regexp to_s form : #{reg_a.to_s}\n\n"
puts "Regexp to_s form : #{reg_b.to_s}\n\n"
输出 :
Regexp to_s form : (?ix-m:geeks)
Regexp to_s form : (?-mix:(.)(.))