[英]Ruby regex parse string for two sets of numbers?
我一直在使用Rubular.com提出一個正則表達式模式,嘗試解析此字符串以獲取值596和777並將它們放入數組中:
Current mouse position: 596,777
我一直在IRB中構建它,並使用t.scan(/(?:\\d)/)
這將導致: ["4", "5", "1", "1", "1", "3", "7"]
不正確。
有人有指針嗎?
str = "Current mouse position: 596,777"
x, y = str.split()[-1].split(",")
puts x, y
--output:--
596
777
...
str = "Current mouse position: 596,777"
x = str[-7..-5]
y = str[-3..-1]
puts x, y
--output:--
596
777
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.