r =/
\b # match a word break
[Bb] # match "B" or "b"
udget: # match string
\s+\$ # match one or more spaces followed by a dollar sign
\K # discard all matches so far
\d{1,3} # match between one or three digits
(?:\,\d{3}) # match a comma followed by three digits in a non-capture group
* # perform the preceding match zero or more times
(?:\.\d\d) # match a period followed by two digits in a non-capture group
? # make the preceding match optional
/x # free-spacing regex definition mode
"Some text Budget: $25\nsome more text"[r] #=> "25"
"Some text Budget: $25.42\nsome more text"[r] #=> "25.24"
"Some text Budget: $25,642,328\nsome more text"[r] #=> "25,642,328"
"Some text Budget: $25,642,328.01\nsome more text"[r] #=> "25,642,328.01"
Это на самом деле не совсем верно, потому что
"Some text Budget: $25,64,328.01\nsome more text"[r] #=> "25"
должен вернуть nil
. К сожалению, исправление требует серьезной операции:
r =/
\b # match a word break
[Bb] # match "B" or "b"
udget: # match string
\s+\$ # match 1 or more spaces followed by a dollar sign
\K # discard all matches so far
\d{1,3} # match between 1 and 3 digits
(?: # begin a non-capture group
(?![\,\d]) # match a comma or digit in a negative lookahead
| # or
(?: # begin a non-capture group
(?:\,\d{3}) # match a comma followed by 3 digits in a non-capture group
+ # perform preceding match 1 or more times
) # end non-capture group
) # end non-capture group
(?:\.\d\d) # match a period followed by 2 digits in a non-capture group
? # make the preceding match optional
/x
"Some text Budget: $25\nsome more text"[r] #=> "25"
"Some text Budget: $25.42\nsome more text"[r] #=> "25.24"
"Some text Budget: $25,642,328\nsome more text"[r] #=> "25,642,328"
"Some text Budget: $25,642,328.01\nsome more text"[r] #=> "25,642,328.01"
"Some text Budget: $25,64,328.01\nsome more text"[r] #=> nil
[ '/Budget(.*)$/'](https://regex101.com/r/wZ2gP9/1)? – Shafizadeh
'str.match (/ Budget: (\ $ [\ d.] +) /) [1]'? – Dogbert
Это хороший старт @ Шафизаде :). Однако мне нужно было бы зафиксировать только строку цены после «Бюджет:». В этом примере это $ 25, но это может быть $ 1000 или $ 10 000 или даже $ 100.54. – Cyzanfar