str = "2[abc]3[ab]c2[de]fg"
str.gsub(/(d+)[(p{L}+)]/) { $2*$1.to_i }
#=> "abcabcabababcdedefg"
# ^^^^^^
# ^^^^^^
# ^^^^
We can write the regular expression in free-spacing mode to make it self-documenting.
/
(d+) # match one or more digits, save to capture group 1
[ # match '['
(p{L}+) # match one or more letters, save to capture group 2
] # match ']'
/x # invoke free-spacing regex definition mode
For the first match,
$1 #=> "2"
$2 #=> "abc"
so the block becomes
"abc"*2
#=> "abcabc"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…