There is a method in an old rails application. it can convert a string into an array by regular expression.
Just like this:
irb(main):047:0> string = "[(a+50%+1)(b+60%+2)]"
=> "[(a+50%+1)(b+60%+2)]"
irb(main):048:0> string.scan(/\((?<name>\w+)\+(?<percent>\d+)%\+(?<num>\d+)\)/)
=> [["a", "50", "1"], ["b", "60", "2"]]
But requirement is changed, I have to add a new value to the string.
I can make it like this:
irb(main):049:0> string = "[(a+50%+1+100)(b+60%+2+200)]"
=> "[(a+50%+1+100)(b+60%+2+200)]"
irb(main):050:0> string.scan(/\((?<name>\w+)\+(?<percent>\d+)%\+(?<num>\d+)\+(?<max>\d+)\)/)
=> [["a", "50", "1", "100"], ["b", "60", "2", "200"]]
but it can't compatible with the previous,
irb(main):051:0> string = "[(a+50%+1)(b+60%+2)]"
=> "[(a+50%+1)(b+60%+2)]"
irb(main):052:0> string.scan(/\((?<name>\w+)\+(?<percent>\d+)%\+(?<num>\d+)\+(?<max>\d+)\)/)
=> []
Is there better way to make it? please help, thanks in advance!