I'm using ruby 2.0
and its regex engine.
Given the following string:
str = "one: two, three: four"
The list's length is variable (from 0 to infinite). How do I capture it's entries? So in this example the regex should match:
[1]:"one: two", [2]:"three: four"
This is what I came up with so far:
/((\w+:\s\w+),?)*/
but it only gives me:
=> #<MatchData "one: two," 1:"one: two," 2:"one: two">
What am I doing wrong?
You don't need regular expression. Use String#split
:
str = "one: two, three: four"
str.split(', ') # => ["one: two", "three: four"]
Using regular expression:
str.split(/, /) # => ["one: two", "three: four"]
str.scan(/[^,]+/) # => ["one: two", " three: four"]
str.scan(/[^,]+/).map &:strip # => ["one: two", "three: four"]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments