當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Regexp source()用法及代碼示例

Regexp#source():source()是Regexp類方法,它返回模式的原始字符串。

用法:Regexp.source()

參數:正則表達式值


返回:模式的原始字符串。

示例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 =/(?<go>.)(?<for>.)(?<it>.)/ 
  
  
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 = /(?<hi>.)(?<there>.)e/ 
  
# declaring Regexp value 
reg_c = /(?<i>.)(?<can>.)(?<code>.)/ 
  
  
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 : (?.)(?.)(?.)




相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Regexp source() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。