On Mon, Sep 3, 2012 at 9:44 AM, Brian Candler <[email protected]> wrote:
> Erez Ben shoham wrote in post #1074385:
>> Hi
>>
>> I have a string
>> how can i extract the number after the # sign?
>
> Use a reegular expression match.
>
>> old_string = "this is _ a 76string / #123 like 456"
>
> if old_string =~ /#([0-9]+)/
>   new_string = $1
> else
>   puts "Couldn't find it"
> end

Or use String#[]:

irb(main):006:0> arr = ["this is _ a 76string / #123 like 456",
irb(main):007:1* "435 bill#198777 bgt",
irb(main):008:1*  "my_name#9_is?"]
=> ["this is _ a 76string / #123 like 456", "435 bill#198777 bgt",
"my_name#9_is?"]
irb(main):009:0> arr.map {|s| s[/(?<=#)\d+/]}
=> ["123", "198777", "9"]

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to