0

How to extract a point separated number format from a string?

for example a string like this

a = 'ProductX credit 1.000'

how to to get only the 1.000 from that string?

Thank you kindly

2
  • 2
    See stackoverflow.com/questions/13706706/…, the regex .match(/\d+.\d+/) is probably the most reliable way, but it also depends on the concrete formatting of the strings and different languages may use different separators (in German it would be , instead of .). Commented Dec 24, 2020 at 9:23
  • 1
    What's the format of the "number" you want to extract? Is it always one digit followed by a . followed by three digits? How do (much) larger or smaller numbers look like? What about negative values? Commented Dec 24, 2020 at 11:16

2 Answers 2

1

You can use method split by space in ruby

a = 'ProductX credit 1.000'
a.split(" ").last

Result

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

1 Comment

The OP said the string was just an example. What if the number doesn't come last?
1

Input

a='ProductX credit 1.000'

Code

p a.rpartition(/\s/).last

Output

"1.000"

2 Comments

That extracts the last word, not necessarily "a point separated number"
You are right. Once you read this comment, I will delete my answer.

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.