Given a variable s containing a string, which might contain an
underscore, I would like to extract the character AFTER the underscore.
I thought this is easy:
after = s[/($<=_)./]
But this returns nil all the time. Indeed, I see that, for example,
‘ab_cd’ =~ /($<=_)./
doesn’t match. Could it be that I don’t understand the proper usage of
positive lookbehind?
=========================================
SOLVED! As B.Onzo pointed out, I had wrong syntax. The correct statement
would be
Given a variable s containing a string, which might contain an
underscore, I would like to extract the character AFTER the underscore.
I thought this is easy:
after = s[/($<=_)./]
But this returns nil all the time. Indeed, I see that, for example,
‘ab_cd’ =~ /($<=_)./
doesn’t match. Could it be that I don’t understand the proper usage of
positive lookbehind?
Maybe, but you aren’t using any positive lookbehind, that would be
(?<=xxx) with a ?
Maybe with you could get away with just using the ‘split’ method.
Of course, this is possible. Actually, as a temporary solution, I used a
regexp, similar to your solution with ‘match’.
However, I thought that this should be possible to solve with a
lookbehind, and would like to use this feature. I’m searching that part
of a string, which comes AFTER a certain pattern. Shouldn’t this be a
canonical example for “positive lookbehind”?