簡體   English   中英

Ruby正則表達式解析字符串為兩組數字?

[英]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"]不正確。

有人有指針嗎?

您忘記了放置一個量詞

t.scan(/\d+/)

量詞+表示一個或多個

注意: 非捕獲組 (?:...)是無用的。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM