On Wednesday 22 July 2009 14:58:47 Stephen Beard wrote:
> Hello,
>
> I am new to ruby and had a question about parsing text.
>
> I am trying to parse out the first IP address from a string. I have a
> working solution, but it seems like a rather round about way to do it.
> This is my current method:
>
> str = "          inet addr:192.168.1.118  Bcast:192.168.1.255
> Mask:255.255.255.0"
> ipAddr = ((str.split(':'))[1].split)[0]
>
> Is there a better way to do this? Thanks in advance.

regexp:
str.scan(/addr:([^\s]+)/)[0][0]

string math:
str.slice(str.index(':')+1, str.index(' B')-str.index(':')-2)