Search code examples
regexreplaceunicodepcreadobe-indesign

Regex: Replace "something" by a unicode character


I am trying to figure out how to find a certain character and replace it with a Unicode character. In my example, I want to find all spaces (\s) and replace them with a narrow or thin space (e.g. Unicode U+2006).


Sample Text

8. 3. 2014


Search Pattern

(\d{1,2}\.)(\s?)(\d{1,2}\.)(\s?)(\d{2,4})


Replacement Pattern

$1{UNICODE}$3{UNICODE}$5


For some reason I cannot replace by(!) a Unicode character, I can only search for one. I am working with a RegEx App called »RegExRX 3« to test my strings. In the end, I want to be able to use it with Adobe’s InDesign GREP functionality.

I know I could just copy and paste the correct whitespace into place but I am interested in how to do it with a Unicode character.

Thanks in advance!


Solution

  • InDesign uses Perl-compatible regular expressions (pcre). Getting a Unicode character into the replacement string is done by \x{XXXX} where XXXX is the hexadecimal character code:

    $1\x{2009}$2\x{2009}$5
    

    But in general you can replace by any character you can type. Just put actual thin spaces into your search-and-replace dialog:

    $1 $3 $5
    

    You can use your OS's utilities to grab the thin space from the list of available characters, for Windows it's the "Character Map" tool, where the thin space can be found in the "General Punctuation" Unicode sub-range. Searching for "thin space" works as well. MacOS has the "Character Viewer", which can do the same thing.

    Thin Space selection on Windows