Hello --

On Fri, 27 Jul 2001, Roger Lipscombe wrote:

> I'm trying to do some regex replacement in strings:
>
> def assertStringsEqual(actual, expected)
>  if not (actual == expected)
>   raise "Assertion failed: actual: '" + actual + "', expected: '" + expected
> + "'"
>  end
> end
>
> # Match things in MixedCase
> def substituteRegex(string)
>  re = /\b([A-Z][a-z]+){2,}\b/

$1 will match what was (last) matched in the first set of parens from
the left.  Since the {2,} is outside the parens, what gets matched
inside is each of the submatches (This, Should, and Work) in
succession.  The final one is Work, so that's what goes into #1.

You need to move the {2,} inside the parens.  Actually you need two
sets of parens, because you also need to group the thing to which
the {2,} refers:

  irb 9> str = "ThisShouldWork"
  irb 10> re = /\b(([A-Z][a-z]+){2,})\b/
  irb 11> str.scan(re)
     ==>[["ThisShouldWork", "Work"]]

and if you want to get rid of that extraneous $2, you can use ?: to
prevent the inner grouping from setting a variable:

  irb 12> re = /\b((?:[A-Z][a-z]+){2,})\b/
  irb 13> str.scan(re)
     ==>[["ThisShouldWork"]]


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav