From: Simon Strandgaard Date: 2004-04-14T07:49:14+09:00 Subject: Re: New Ruby questions... On Tue, 13 Apr 2004 23:10:11 +0000, Jeff Massung wrote: [snip] > Quickly, the "teach myself" project is a simple ARM assembler. I've done > it numerous times, and it is something I'm familiar and comfortable with > (let alone something I need ATM). > sounds interesting... BTW: are you to follow test-first paradigm? > 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). > There is many ways to do this.. I can recommend you to read up on 'lexing'.. a phase where you transform the sourcecode into tokens, where all whitespace is stripped off. Then you have your registernames as you want them. However you asked for regexp.. then you can place word-boundary anchors around the registers. Also place the longest names first in the alternation. But I rather recommend the lexing path. registers = / \b (?: r10 | r11 | r12 | ... r0 | r1 | r2 ) \b /x BTW: welcome to Ruby ;-) -- Simon Strandgaard