DDDD DDDD - 1 year ago 59
Java Question

Add percent character after every second character with Regex

I want to create a regex that will add the percent character '%' in front of every second character in a Hex string. I also would like a leading percentage character.

For example, the Hex '54657374696E67' in ASCII is 'testing'. In my query I need it to end up being '%54%65%73%74%69%6E%67'.

Can a regex do this? I am using Java's regex library.

sln sln
Answer Source

Find (?i)([a-f0-9]{2})
Replace %$1