1
如何將一個命名組與x
正則表達式修飾符一起使用?命名組+ x修飾符
作品:
/(?<number>\d+)/ =~ 'hello here is a number 3298472398723'
puts number
作品:
regexp =/
number # this is a word
\s # this is a space
(\d+) # this is a bunch of numbers
/x
regexp =~ 'hello here is a number 3298472398723'
puts $1
不起作用:
regexp =/
number # this is a word
\s # this is a space
(?<number>\d+) # this is a bunch of numbers
/x
regexp =~ 'hello here is a number 3298472398723'
puts number
我在做什麼錯?
從[documentation](http://ruby-doc.org/core-2.1.1/Regexp.html#method-i-3D-7E):*「正則表達式必須是沒有插值的文字並放在左側。「* – Stefan
@Stefan謝謝,更新了一個答案。 – mudasobwa
@ Fuser97381我的名字不是馬茨,順便說一句。 – mudasobwa