1

Messing around with Groovy for a work project I ran into this oddity:

def string = "__RNDHPD(70.2300000..70.2310000)"
def params = []
string.find(/(?<=\().*?(?=\))/).tokenize("..").each { params << it.trim() }
// should yield [70.2300000,70.2310000] but instead results in [70, 2300000, 70, 2310000]

Using an alternative token works fine though. I don't think I am doing anything wrong, perhaps someone could shed some light on whether this my problem or something I should report to the Groovy devs.

Philip Lombardi
  • 1,276
  • 2
  • 17
  • 26

1 Answers1

2

It's not a bug, the documentation is just very poor. The tokenize method is just a wrapper around StringTokenizer, so the string you are passing to it is actually a list of delimiter characters. Try the split method instead.

def string = "__RNDHPD(70.2300000..70.2310000)"
def params = []
string.find(/(?<=\().*?(?=\))/).split(/\.\./).each { params << it.trim() }

assert params == ['70.2300000','70.2310000']
Justin Piper
  • 3,154
  • 1
  • 20
  • 14