Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

YANQ (yet another newbie question): "string"[0.."e"]

0 views
Skip to first unread message

basi

unread,
Aug 13, 2005, 11:55:13 PM8/13/05
to
Hello,

Is there a way to say in Ruby:

"astring"[0../r/] #"ast"
"astring"[/ast/..-1] #"ring"

In other words, indexing using a "pattern".

Thanks,
basi

Joel VanderWerf

unread,
Aug 14, 2005, 12:03:27 AM8/14/05
to

You just have to tinker with the pattern a bit:

"astring"[/.*?(?=r)/] # ==> "ast"
"astring"[/ast(.*)/, 1] # ==> "ring"

The (?=r) is a lookahead operator that matches but does not consume
characters. The numerical argument n=1 in the second example indicates
that #[] should return the value of the n-th capture

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


William James

unread,
Aug 14, 2005, 12:10:45 AM8/14/05
to

"astring"[/.*?(?=r)/] --> "ast"
"astring"[/(.*?)r/,1] --> "ast"

"astring"[/ast(.*)/,1] --> "ring"

basi

unread,
Aug 14, 2005, 12:49:50 AM8/14/05
to
To Joel V and William J.
I didn't think this was possible and I'm glad I asked. I'm impressed!
Thanks for the help.
basi

Vance Heron

unread,
Aug 14, 2005, 3:34:43 AM8/14/05
to
basi wrote:

I can't do it with a literal string, but think
this is close.

irb(main):004:0> irb
irb#1(main):001:0> s = 'astring'
=> "astring"
irb#1(main):002:0> s[0..s.index('r')]
=> "astr"
irb#1(main):003:0> s[s.index('ast')..-1]
=> "astring"
irb#1(main):004:0> s[s.index('rin')..-1]
=> "ring"

Note that the index for 'ast' is really 0,
so [/ast/..-1] turns into [0..-1].

HTH,
Vance


Robert Klemme

unread,
Aug 14, 2005, 9:26:13 AM8/14/05
to

Interestingly nobody seems to care to anchor the expression at the beginning
of the string. Here are other variants that do this

>> "astring"[/\A[^r]*/]
=> "ast"
>> "astring"[/\A.*?(?=r)/]
=> "ast"
>> "astring".match(/\A.*?(?=r)/)[0]
=> "ast"

Kind regards

robert

basi

unread,
Aug 14, 2005, 11:41:57 AM8/14/05
to
Wonders never cease! A very readable solution, and it stays within
basic Ruby constructs. Well within the color of my Ruby belt at this
point. I'll try it.

Though I will keep in mind the regex-based solution suggested above, as
an example of what can be done for when the pattern gets complex.

Thanks all.
basi

William James

unread,
Aug 15, 2005, 2:44:51 AM8/15/05
to

basi wrote:
> Hello,
>
> Is there a way to say in Ruby:
>
> "astring"[0../r/] #"ast"
> "astring"[/ast/..-1] #"ring"

Another approach is to remove what you don't want:

"astring".sub(/r.*/,'') --> "ast"

"astring".sub(/^ast/,'') --> "ring"

0 new messages