DDDD DDDD - 2 months ago 9
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

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

Comments