有沒有一種方法可以在Ruby中通過組名來執行使用分組正則表達式的替換?用Ruby中的組名稱替換分組的正則表達式
這是我到目前爲止有(但你會看到它缺少這使得一些有價值的環境下很常見的情況是無用的):
class String
def scan_in_groups(regexp)
raise ArgumentError, 'Regexp does not contain any names.' if regexp.names.empty?
captures = regexp.names.inject({}){ |h, n| h[n] = []; h }
scan(regexp).each do |match|
captures.keys.zip(match).each do |group, gmatch|
next if !gmatch
captures[group] << gmatch
end
end
captures.reject { |_, v| v.empty? }
end
def sub_in_groups(regexp, group_hash)
dup.sub_in_groups!(regexp, group_hash)
end
def sub_in_groups!(regexp, group_hash)
scan_in_groups(regexp).each do |name, value|
next if !group_hash[name]
sub!(value.first, group_hash[name])
end
self
end
end
regexp =/
\/(?<category>\w+) # matches category type
\/ # path separator
(?<book-id>\d+) # matches book ID numbers
\/ # path separator
.* # irrelevant
\/ # path separator
chapter-(?<chapter-id>\d+) # matches chapter ID numbers
\/ # path separator
stuff(?<stuff-id>\d+) # matches stuff ID numbers
/x
path = '/book/12/blahahaha/test/chapter-3/stuff4/12'
p path.scan_in_groups(regexp)
#=> {"category"=>["book"], "book-id"=>["12"], "chapter-id"=>["3"], "stuff-id"=>["4"]}
update = {
'category' => 'new-category',
'book-id' => 'new-book-id',
'chapter-id' => 'new-chapter-id',
'stuff-id' => '-new-stuff-id'
}
p path.sub_in_groups(regexp, update)
#=> "/new-category/new-book-id/blahahaha/test/chapter-new-chapter-id/stuff-new-stuff-id/12"
p '/12/book/12/blahahaha/test/chapter-3/stuff4/12'.sub_in_groups(regexp, update)
#=> /new-book-id/new-category/12/blahahaha/test/chapter-new-chapter-id/stuff-new-stuff-id/12
我需要的是一個解決方案,蜜餞正則表達式匹配的背景和obly替換它們,這樣最終的結果將是:
#=> /12/new-category/new-book-id/blahahaha/test/chapter-new-chapter-id/stuff-new-stuff-id/12
這可能嗎?
你錯過了「更新」的說法,但比它似乎只是正常工作等,謝謝。 :) –