2

I have the following string:

WOA: 1234-123 - Text Text Text Text

or

WOA 0123 Text Text Text

or

WOA 012345678 - Text Text Text

I need to get only Numbers from those strings (If number contains dashes inside I need those dashes too, but if dash at the end, I don't need it). I know that I could do it using ASCII codes, but with Regex it will looks much better, but my knowledge in Regex very limited, so any help, please.

1
  • 1
    Show your best attempt. Commented Nov 8, 2013 at 14:25

1 Answer 1

7

Probably you're looking for this regex:

[0-9]+(-[0-9]+)*
Sign up to request clarification or add additional context in comments.

7 Comments

I would suggest to put in a + char instead of ?: \d+(-\d+)+. Now it will also match on 123-456-78. TS state explicitly multiple dashes: "If number contains dashes inside I need those dashes too"
Actually [0-9]+(-[0-9]+)* will be more accurate since not always hyphen will be there
You all should quit ninja editing your posts after I commented on it! :)
Deleted mine, because this is superior.
Thanks guys! It works, but who voted down my question? What's wrong with it?
|

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.