红宝石 |正则表达式 source()函数
Regexp#source() : source()是一个 Regexp 类方法,它返回模式的原始字符串。
Syntax: Regexp.source()
Parameter: Regexp values
Return: original string of the pattern.
示例 #1:
# Ruby code for Regexp.source() method
# declaring Regexp value
reg_a = /a/
# declaring Regexp value
reg_b = /\xa1\xa2/e
# declaring Regexp value
reg_c =/(?.)(?.)(?.)/
# source method
puts "Regexp source form : #{reg_a.source}\n\n"
puts "Regexp source form : #{reg_b.source}\n\n"
puts "Regexp source form : #{reg_c.source}\n\n"
输出 :
Regexp source form : a
Regexp source form : \xa1\xa2
Regexp source form : (?.)(?.)(?.)
示例 #2:
# Ruby code for Regexp.source() method
# declaring Regexp value
reg_a = /geeks/ix
# declaring Regexp value
reg_b = /(?.)(?.)e/
# declaring Regexp value
reg_c = /(?.)(?.)(?.)/
# source method
puts "Regexp source form : #{reg_a.source}\n\n"
puts "Regexp source form : #{reg_b.source}\n\n"
puts "Regexp source form : #{reg_c.source}\n\n"
输出 :
Regexp source form : geeks
Regexp source form : (?.)(?.)e
Regexp source form : (?.)(?.)(?.)