From: Mark Hubbart Date: 2004-04-14T07:59:13+09:00 Subject: Re: New Ruby questions... On Apr 13, 2004, at 3:14 PM, Jeff Massung wrote: > I've just started Ruby a couple days ago welcome! :) > 1. How can a regexp get the longer of two possibilities that are > ambiguous? For example, I need to be able to strip out register names: > > registers = '(r0|r1|r2|r3...|r10|r11|r12...)' > > In the regular expression, how can I get it to find r10 or r11 instead > of > stopping at r1? I get the same problem with opcode mnemonics (like B > instead of BX or BL). Are you wanting a regex that matches register names? here's a couple ways: # A bit lazy, but less complicated: matches 1 or 2 digit numbers # the \d means match any single digit; following the second \d with # a ? means that it should match it if it's there, but it is optional. register_regex = /r\d\d?/ # A bit less more complex, but more precise; matches up to "r42" # rough translation: "match an 'r', followed by either the a digit # followed by a non-digit; or a number 1-3 followed by one digit # and a non-digit; or the number 4 followed by a 0, 1, or 2. register_regex = /r(?:\d\D|[1-3]\d\D|4[012]\D)/ If you are going to be doing lots of regex stuff, I highly reccoment going out and getting a small pocket manual regex reference. When I started working with them, I got a little perl-oriented one, but it works just as well for Ruby, PHP, or most other implementations. I just marked it up to show what was available in each language. cheers, --Mark