Using Ruby to scan through a string

Posted by nekosune on Stack Overflow See other posts from Stack Overflow or by nekosune
Published on 2012-11-09T15:48:57Z Indexed on 2012/11/11 5:01 UTC
Read the original article Hit count: 234

Filed under:
|

I am trying to create a regex to gather info from strings that look like this:

A22xB67-E34...

for any number.

I have the regex:

@spaceCode = "[A-Z]([A-Z0-9][0-9]|[0-9])"
@moveCode=/^(?<one>#{@spaceCode})((?<mode>x|\-)(?<two>#{@spaceCode}))+$/

However I get:

s="A11-A22xA33".scan(@moveCode)
=> [["A11", "11", "xA33", "x", "A33", "33"]]

which is most definatly NOT what I want.

The string could be any length of C22 etc, with either x or - as the seperator, and put it into an array like:

['A22','x',B22','-'.......]

Examples:

"A22xB23-D23xE25" => ['A22','x','B23','=','D23','E25;]
"AA2xA9-A1" => ['AA2','x','A9','-','A1']

© Stack Overflow or respective owner

Related posts about ruby

Related posts about regex