Considering this string:
Looking for a front-end developer who can fix a bug on my Wordpress site. The header logo disappeared after I updated some plugins. \n\nI have tried disabling all plugins but it didn't help.Budget: $25\nPosted On: May 06, 2016 16:29 UTCCategory: Web, Mobile & Software Dev > Web DevelopmentSkills: WordPress Country: Denmarkclick to apply
I'd like to retrieve the price value after the string Budget:
. I have a number of string all with the same pattern (price right after the "Budget:" string)
I tried /\$[\d.]+/
to extract any price amount but that would take any price amount in the string not only the one following Budget:
How can I accomplish that ?
答案 0 :(得分:3)
EraseView
这实际上并不完全正确,因为
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
答案 1 :(得分:1)
试试这个:
def extract_budget s
m = s.match(/Budget: \$([\d,.]+)\n/)
if m.nil?
nil
else
m.captures[0].gsub(/,/, "").to_f
end
end
如果s1
是您的字符串,而s2
是相同的字符串,但是"预算:$ 25,000.53":
irb> extract_budget s1
=> 25.0
irb> extract_budget s2
=> 25000.53
irb> extract_budget "foo"
=> nil
答案 2 :(得分:1)
你说字符串"预算:"不会改变并假设没有小数值,我会使用这样的东西:
/Budget:(\s*\$\d*)/