groovygroovyshellgroovy-console

Need to convert a string "4AAC6AA8D5827BA" to into this format "4a:ac:6a:a8:d5:82:7b" using groovy


I am trying to convert this string string "4AAC6AA8D5827BA" to into this format "4a:ac:6a:a8:d5:82:7b" using groovy. Could you please asssit


Solution

  • This should do the trick:

    def addDots(x) { x.toLowerCase().replaceAll(/([0-9a-f]{2})(?!$)/, '$0:') }
    

    This doesn't check that the input is of the correct pattern and will do weird things to anything in a different format.

    But addDots("4AAC6AA8D5827BA") will return 4a:ac:6a:a8:d5:82:7b:a

    Note that the input you gave has an odd number of characters, which is probably a copy-paste error somewhere.