0

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']
7
  • Trying this in IRB, I got: "RegexpError: undefined (?...) sequence: /^(?<one>[A-Z]([A-Z0-9][0-9]|[0-9]))((?<mode>x|\-)(?<two>[A-Z]([A-Z0-9][0-9]|[0-9])))+$/" Commented Nov 9, 2012 at 15:50
  • @JohnDibling It works fine through irb on rails with me :S Commented Nov 9, 2012 at 15:52
  • @MartinVidner I was hoping I could get an array of something like ['A11',['-','A22','x','A33']] Commented Nov 9, 2012 at 15:53
  • I have to admit that I don't really know what "irb on rails" means. :( Commented Nov 9, 2012 at 15:56
  • that is expected since u r using named groups which itself contains a group Commented Nov 9, 2012 at 15:57

4 Answers 4

2

Presumably you want to find these three alphanumeric codes in isolation? Would this simpler regex (or a variant on it) do what you want?

def decode string
  puts "\nDecoding #{string}"
  code = "[A-Z0-9]{1,3}"
  sep = "[-x=]"
  r = /(?:(#{code})#{sep}?)?/
  string.scan(r)
end

puts decode("A22xA33")
puts decode("A11-A22xA33")
puts decode("A22xB67-E34")
puts decode("A22xC33xD44-E55")
puts decode("A22xB23-D23=E25")

or if you want to capture actions/separators too, something like:

r = /(?:#{code}#{sep}?)?/

or to capture actions/separators in the array as separate items between the codes:

r = /(#{code})?(#{sep})?/

So I think what you want is:

def decode string
  puts "\nDecoding #{string}"
  code = "[A-Z0-9]{1,3}"
  sep = "[-x=]"
  r = /(#{code})?(#{sep})?/
  string.scan(r)
end

or in its simplest form just:

string.scan(/([A-Z0-9]{1,3})?([-x=])?/)
Sign up to request clarification or add additional context in comments.

3 Comments

Afraid not. basically this is for a variable sequence of moves, that can either be move, or captre, so it could look like AA2xA33 or AA2-A33xB44 or even A22xC33xD44-E55
Edited reply to deal with variable length patterns. I think this matches your examples. You should put a list of examples to match in your question to make it clearer.
let me do that, sorry for the trouble caused here, and thankyou
1

I'd do it this way:

MOVE_REGEX = /[a-z]+\d+/i
REGEX = /(#{ MOVE_REGEX })([x-]?)/i

class String
  def parse_move
    self.scan(REGEX).flatten.reject(&:empty?)
  end
end

"A22xB23-D23xE25".parse_move
# => ["A22", "x", "B23", "-", "D23", "x", "E25"]

"AA2xA9-A1".parse_move
# => ["AA2", "x", "A9", "-", "A1"]

Comments

0

Seems like this should work, given that field names can be variable length:

def parse_moves(s)    
  s.scan(/([A-Z0-9]+)?([-x])?/).flatten.compact
end

Results for your examples:

1.9.3-p125 :027 > moves = ["A11-A22xA33", "A22xB23-D23xE25", "AA2xA9-A1"]
 => ["A11-A22xA33", "A22xB23-D23xE25", "AA2xA9-A1"] 
1.9.3-p125 :028 > moves.each { |s| puts parse_moves(s).to_s }
["A11", "-", "A22", "x", "A33"]
["A22", "x", "B23", "-", "D23", "x", "E25"]
["AA2", "x", "A9", "-", "A1"]

Comments

0

Use method like split with the regex as ([x-])


Your regex won't work for your input since this would always overwrite the groups spaceCode and mode, i.e. spaceCode and mode would contain only the last matched spaceCode and mode.

For example, the regex ^(\d)+$ for input 3664 would capture only 4 not 3,6,6,4 in the group.

1 Comment

That gets it closer, but still only captures the first and last of the regex

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.