I have a string:
s = "<aaa>bbb</ccc>"
I want to get aaa and bbb in ruby block for sub method.
If I call:
s.sub(/<([a-z]+)>([\s\S]+)<\/[a-z]+>/,"first=\\1 second=\\2")
everything works as I expect, so the output is “first=aaa second=bbb”.
Then I call the same regexp with ruby-block, but it returns only the whole string but not \\1 and \\2 parts:
s.sub(/<([a-z]+)>([\s\S]+)<\/[a-z]+>/) { |x,y| puts x; puts y; }
This case output is
<aaa>bbb</ccc>, nil.
How can I get sentences in braces like \\1, \\2 in ruby-block for sub method?
The string yielded by
subwill always be the full match. To get at the captures, you can use$1and$2inside the block.