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/
string.gsub(re) {
"prolog:" + $1 + ":middle:" + $1 + ":epilog"
}
end
assertStringsEqual(substituteRegex("ThisShouldWork"),
"prolog:ThisShouldWork:middle:ThisShouldWork:epilog")
assertStringsEqual(substituteRegex("THISSHOULDNOTWORK"),
"THISSHOULDNOTWORK")
And it's not working. I get the following:
./test.rb:301:in `assertStringsEqual': Assertion failed: actual:
'prolog:Work:middle:Work:epilog', expected:
'prolog:ThisShouldWork:middle:ThisShouldWork:epilog' (RuntimeError)
Essentially, the {2,} doesn't seem to be working, and it's grabbing the last
piece that matches.
Ideas?
Cheers,
Roger.