0

I have been using Rubular.com to come up with a regex pattern to try to parse this string for the values 596 and 777 and put them in an array:

Current mouse position: 596,777

I have been building this in IRB and testing it with t.scan(/(?:\d)/)

This yields: ["4", "5", "1", "1", "1", "3", "7"] which is not correct.

Does anyone have any pointers?

2 Answers 2

3

You have forgotten to put a quantifier:

t.scan(/\d+/)

The quantifier + means one or more

Note: The non-capturing group (?:...) is useless.

Sign up to request clarification or add additional context in comments.

Comments

-1
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.