Sam Sweeney Sam Sweeney -5 years ago 92
Ruby Question

Regex in ruby. Selecting string from string with different amount of characters

I'm trying to select a number which varies in length from a string.

The number can be anywhere between 5 and 10 characters long.

For example, I have two different lengths of 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