Regular expression to return numbers of string
I need an regular expression to return numbers of given string when it
match some of these patterns:
Number followed by x OR
some or one number separated by at least one space
Examples:
' 30x ' # should match
'30x' # should match
'20 30' # should match
' 20 30 ' # should match
'20x 30 ' # should not match
'x20 ' # should not match
I looking for some regex that will be used with match method in ruby:
regex = ???
'30x'.match(regex).to_a #=> ['30']
'30 40'.match(regex).to_a #=> ['30', '40']
Thank you!
Edit:
I'll add some more examples for clarity:
> "30".match(regex).to_a
=> ["30"]
> " 30 ".match(regex).to_a
=> ["30"]
> "30 40".match(regex).to_a
=> ["30", "40"]
> "30xx".match(regex).to_a
=> nil
> "30 a".match(regex).to_a
=> nil
> "30 60x".match(regex).to_a
=> nil
> "30x 20".match(regex).to_a
=> nil
I dont know if method match is the correct to achieve these results.
No comments:
Post a Comment