From: Mike Stok Date: 2012-04-01T21:45:14+09:00 Subject: Re: Ruby regex match hex string Note that hexadecimal digits can be matched by a POSIX character class, or in 1.9 there is a special escape for them: /\A(?:[[:xdigit:]]{1,2},)*[[:xdigit:]]{1,2}\z/ # 1.8, 1.9 or /\A(?:\h{1,2},)*\h{1,2}\z/ # 1.9 would match a valid string. Mike On 2012-03-31, at 4:15 PM, Peter Vandenabeele wrote: > On Sat, Mar 31, 2012 at 8:31 PM, Jan E. wrote: >> Hi, >> >> What makes you think that only the last two pairs are matched? This is >> impossible, because you're using the \A and \Z anchors. Either the whole >> string matches or it doesn't match at all. >> >> The regex is actually correct and it does match the example string. > > The confusion is that only two capturing groups are returned > (while the first part between brackets matches many times). > > 1.9.3p125 :001 > s = "11,1,aa,a,1b,3b,55,b6" > => "11,1,aa,a,1b,3b,55,b6" > 1.9.3p125 :002 > s.match(/\A([0-9a-fA-F]{1,2},{1})*([0-9a-fA-F]{1,2}){1}\Z/) > => # > 1.9.3p125 :003 > s.match(/\A([0-9a-fA-F]{1,2},)*([0-9a-fA-F]{1,2})\Z/) > => # > > And as mentioned by gabe, maybe scan is what the OP was after; > > 1.9.3p125 :004 > s.scan(/[0-9a-f]{1,2}/i).inspect > => "[\"11\", \"1\", \"aa\", \"a\", \"1b\", \"3b\", \"55\", \"b6\"]" > > 1.9.3p125 :005 > s.scan(/([0-9a-f]{1,2}),/i).inspect # more selective > on the comma > => "[[\"11\"], [\"1\"], [\"aa\"], [\"a\"], [\"1b\"], [\"3b\"], [\"55\"]]" > > HTH, > > Peter -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.