Reputation: 5531
My string could look like this:
Rental DurationPrice60-day$47.8090-day$56.23120-day$60.92
or
Rental DurationPrice90-day$47.80180-day$56.23
or
any combination of:
30-day and/or 60-day and/or 90-day and/or 180-day
I'd like to capture the prices that follow these time frames.
Upvotes: 1
Views: 340
Reputation: 160581
Rather than use a more complicated regex, I'd go with String.scan
and let it walk through the string:
'Rental DurationPrice60-day$47.8090-day$56.23120-day$60.92'.scan(/\$[\d.]+/)
[
[0] "$47.8090",
[1] "$56.23120",
[2] "$60.92"
]
If you don't want the currency indicator you can post-process the values, stripping off the first character:
'Rental DurationPrice60-day$47.8090-day$56.23120-day$60.92'.scan(/\$[\d.]+/).map{ |s| s[1..-1] }
[
[0] "47.8090",
[1] "56.23120",
[2] "60.92"
]
Or use a tiny different regex with the flatten
method:
'Rental DurationPrice60-day$47.8090-day$56.23120-day$60.92'.scan(/\$([\d.]+)/).flatten
[
[0] "47.8090",
[1] "56.23120",
[2] "60.92"
]
Upvotes: 1
Reputation: 126742
This regex captures the prices following all nn-day fields.
/(?:30|60|180)-day\$(\d+\.\d\d)/
Use it as
prices = "Rental DurationPrice60-day$47.8090-day$56.23120-day$60.92"
prices.scan(/(?:30|60|180)-day\$(\d+\.\d\d)/)
to fetch all prices into an array.
Upvotes: 2
Reputation: 8434
This regex will capture all the prices: (?:\d{2,}-day\$(\d+\.\d{2}))
Upvotes: 0