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”.
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”
This works very nice. I’m see new methods I can read up on. Thank you
for the assist.
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”
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.)
On Wed, Nov 23, 2011 at 9:28 AM, Ian M. Asaff [email protected]
wrote:
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 Nov 23, 2011, at 10:14, Hassan S. [email protected]
wrote:
Or on the off chance there might be additional slashes in there –
s.split(’/’).last.strip
File.basename(s).strip