Using a fragment for positional extraction
If you always want to extract the 4th element (including the scheme) from the URI and are sure that your data is regular, you can use Array #slice as follows.
'http://www.example.com/value/1234/different-value'.split('/').slice 4
This will work reliably whether there is a trailing slash or not, if you have more than 4 elements after the split, and whether this fourth element is always strictly numeric. It works because it is based on the position of the element within the path, and not on the contents of the element. However, you will get nil if you try to parse URIs with fewer elements, such as http://www.example.com/1234/
.
Use Scan / Match to retrieve a pattern
Alternatively, if you know that the element you are looking for is always the only one consisting solely of numbers, you can use String # match with look-arounds to extract only the numeric part of the string.
'http://www.example.com/value/1234/different-value'.match %r{(?<=/)\d+(?=/)}
To bind an expression to a path, look-behind and look-ahead statements are required. Without them, you will also find things like w3.example.com
. This solution is the best approach if the position of the target element can change, and if you can guarantee that your element of interest will be the only one that matches the anchor regular expression.
If there are multiple matches (e.g. http://www.example.com/1234/5678/
), you can use String # scan instead to select the first or last match. This is one of those things that โknow your dataโ; if you have irregular data, regular expressions are not always the best choice.
source share