Sam Sweeney Sam Sweeney - 1 year ago 79
Ruby Question

Selecting string of variable length from string

I'm trying to select a number from a string. The number can be between 5 and 10 characters long. For example, I have two job numbers to extract from a string.

"job number:45678 date:01/01/16" # => jobnumber = "45678"
"job number:123421312 date:03/03/16" # => jobnumber = "123421312"

Answer Source

looks like this just works:

"job number:45678 date:01/01/16"[/\d+/]
 => "45678" 

"job number:123421312 date:03/03/16"[/\d+/]
 => "123421312" 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download