Forum: Ruby Help! My positive lookbehind doesn't work!

0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-11-11 17:21
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

   after = s[/(?<=_)./]
C2d1339038b5fec16b5b15878f648d96?d=identicon&s=25 Jesus Castello (jesus_castello)
on 2016-11-11 21:31
Hey Ronald,

Maybe with you could get away with just using the 'split' method. Could
you try the following code & see if it works for your use case?

##########
s = 'ab_cd'
s.split('_').last
##########

If you only want one character after the underscore:

##########
s = 'ab_cd'
s.split('_').last[0]
##########

Another alternative:
(I'm assuming you want the whole string if there is no underscore)

##########
match               = string.match(/\w*_(.)/)
character_or_string = match ? match[1] : string
##########

Let me know if that helps :)
- Jesus Castello, blackbytes.info
150e3d60e1da33d0ef45a3fab66d6019?d=identicon&s=25 B. Onzo (bonzo_goat)
on 2016-11-14 10:00
Ronald Fischer wrote in post #1185221:
> 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 `?`
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-11-14 12:02
Jesus Castello wrote in post #1185225:
> 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"?

Ronald
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-11-14 12:05
B. Onzo wrote in post #1185235:
> Maybe, but you aren't using any positive lookbehind, that would be
> (?<=xxx) with a `?`

Arrrgh!!! You are right, what silly mistake. Indeed, if I replace the
dollar by a question mark, it works!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.