Groovy web console

subscribe to the feed Subscribe
to this
site
Fixed Width Parsing (via #groovywebconsole)
tweet this snippet Tweet
this
script

Fixed Width Parsing

Published 2 months ago by Sohrab
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def ignoreTrailing = true
def fields = [
    [ 'field1', 3 ],
    [ 'field2', 5 ],
    [ 'field3', 7 ],
]

def x = '12345678901234567890'

def pattern = fields.collect { name, size -> "(?<${name}>.{${size}})" }.join() + (ignoreTrailing ? '.*' : '')
def matcher = x =~ pattern
if (matcher.matches()) {
    println
    println matcher.group('field1')
    println matcher.group('field1')
    println matcher.group('field1')
}