Half-space in regex

python, regex, unicode

Solution

Python adds a separator for arguments of print function, you can control this with `sep` argument, try

print ('He is a', '\u200c', 'boy', sep="")

For a pattern, try

new_pattern = '\\1\u200c\\2'

or

new_pattern = '\\1\N{ZERO WIDTH NON-JOINER}\\2'

reason is that when you add an `r` prefix, escapes `\` are ignored, so `\u200c` part of pattern is threated as 5 charactes string, i.e. pattern equals `\\1\\u200c\\2`, hence your error.

Problem

I am supposed to write a little program that takes in a Persian text and in some places changes the space to half-space. The half-space or a zero-width non-joiner is used in some languages to avoid a ligature when normalizing a text. It's unicode character is supposedly `'\u200c'` and in some text-editors it can be shown on the screen with a SHIFT+SPACE: ``` import re txt = input('Please enter a Persian text: ') original_pattern = r'\b(\w+)\s*(ها|هايي|هايم|هاي)\b' new_pattern = r'\1 \2' new_txt = re.sub (original_pattern, new_pattern, txt) print (new_txt) ``` In the code above, `new_pattern` is supposed to introduce a half-space between `\1` and `\2`, currently there is a space between them. The question is: How can I put a half-space there? I tried the following and in both cases got a syntax error: ``` new_pattern = ur'\1\u200c\2' new_pattern = r'\1\u200c\2' ``` By the way, although in the Wikipedia article the unicode character for ZWNJ is given as U+200c, it doesn't seem to be working that way in the python shell and it is actually doubling the space: ``` >>> print ('He is a',u'\u200c','boy') He is a ‌ boy >>> print ("کتاب",u"\u200c","ها") کتاب ‌ ها >>> print ("کتاب ها") کتاب ها >>> ```

Original source