Lacking in regular expressions knowledge, I am having difficulty
replacing the string as shown:
"(linux) alpha/beta"
Essentially, there may be spaces such as:
" ( linux)alpha / beta" or maybe "(linux) alpha / beta"
What I am trying to replace that whole string with is just the last word
after the / so my string will just be "beta".
···
--
Posted via http://www.ruby-forum.com/ .
Ian
(Ian)
23 November 2011 17:23
2
something like this?
ruby-1.9.2-p290 :016 > s = "(linux) alpha / beta"
=> "(linux) alpha / beta"
ruby-1.9.2-p290 :017 > s.slice(s.index("/")+1,s.length).strip
=> "beta"
···
On Wed, Nov 23, 2011 at 12:13 PM, skolo pen <skolopen@yahoo.com> wrote:
Lacking in regular expressions knowledge, I am having difficulty
replacing the string as shown:
"(linux) alpha/beta"
Essentially, there may be spaces such as:
" ( linux)alpha / beta" or maybe "(linux) alpha / beta"
What I am trying to replace that whole string with is just the last word
after the / so my string will just be "beta".
--
Posted via http://www.ruby-forum.com/\ .
This works very nice. I'm see new methods I can read up on. Thank you
for the assist.
···
--
Posted via http://www.ruby-forum.com/ .
Ian
(Ian)
23 November 2011 17:28
4
this is simpler...
ruby-1.9.2-p290 :023 > s = "(linux) alpha / beta"
=> "(linux) alpha / beta"
ruby-1.9.2-p290 :024 > s.split("/")[1].strip
=> "beta"
···
On Wed, Nov 23, 2011 at 12:23 PM, Ian M. Asaff <ian.asaff@gmail.com> wrote:
something like this?
ruby-1.9.2-p290 :016 > s = "(linux) alpha / beta"
=> "(linux) alpha / beta"
ruby-1.9.2-p290 :017 > s.slice(s.index("/")+1,s.length).strip
=> "beta"
On Wed, Nov 23, 2011 at 12:13 PM, skolo pen <skolopen@yahoo.com> wrote:
> Lacking in regular expressions knowledge, I am having difficulty
> replacing the string as shown:
>
> "(linux) alpha/beta"
>
> Essentially, there may be spaces such as:
>
> " ( linux)alpha / beta" or maybe "(linux) alpha / beta"
>
> What I am trying to replace that whole string with is just the last word
> after the / so my string will just be "beta".
>
> --
> Posted via http://www.ruby-forum.com/\ .
>
>
If you are regexically-inclined:
"(linux) alpha / beta"[/(?<=\/).+$/] #=> " beta"
If only there were variable-length look-aheads, then it could be (?<=\/ ?),
for what I assume is a possible case of "alpha/beta".
(I don't recommend doing it this way, necessarily, just throwing it out
there.)
this is simpler...
ruby-1.9.2-p290 :023 > s = "(linux) alpha / beta"
=> "(linux) alpha / beta"
ruby-1.9.2-p290 :024 > s.split("/")[1].strip
=> "beta"
Or on the off chance there might be additional slashes in there --
s.split('/').last.strip
Alternately:
s.scan(/(\w+)\Z/).first.first
TMTOWTDI!
···
On Wed, Nov 23, 2011 at 9:28 AM, Ian M. Asaff <ian.asaff@gmail.com> wrote:
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan