Python: find a series of Chinese characters within a string and apply a function
<p>I've got a series of text that is mostly English, but contains some phrases with Chinese characters. Here's two examples:</p>

<pre><code>s1 = "You say: ä½ å¥½. I say: åè¦"
s2 = "ç­æ¡, my friend, å¨é¢¨å¨å¹"
</code></pre>

<p>I'm trying to find each block of Chinese, apply a function which will translate the text (I already have a way to do the translation), then replace the translated text in the string. So the output would be something like this:</p>

<pre><code>o1 = "You say: hello. I say: goodbye"
o2 = "The answer, my friend, is blowing in the wind"
</code></pre>

<p>I can find the Chinese characters easily by doing this:</p>

<pre><code>utf_line = s1.decode('utf-8') 
re.findall(ur'[\u4e00-\u9fff]+',utf_line)
</code></pre>

<p>...But I end up with a list of all the Chinese characters and no way of determining where each phrase begins and ends.</p>

<p>A possible solution is to capture everything, but in different capture groups, so you can differentiate later if they're in Chinese or not.</p>

<pre><code>ret = re.findall(ur'([\u4e00-\u9fff]+)|([^\u4e00-\u9fff]+)', utf_line)
result = []
for match in ret:
    if match[0]:
        result.append(translate(match[0]))
    else:
        result.append(match[1])

print(''.join(result))
</code></pre>

python regex