简体   繁体   English

带有日文字符的 re.sub

[英]re.sub with Japanese Characters

I have the following string:我有以下字符串:

s = u'アガサ・クリスティー 奥さまは名探偵 ~パディントン発4時50分~(字幕版)'

However, when I try and get rid of the character and everything after it, it doesn't match:但是,当我尝试删除字符及其后的所有内容时,它不匹配:

>>> print re.sub(r'\(.+$', '', s)
アガサ・クリスティー 奥さまは名探偵 ~パディントン発4時50分~(字幕版)

How would I get the string to be just:我将如何让字符串只是:

アガサ・クリスティー 奥さまは名探偵 ~パディントン発4時50分~

? ?

You should ensure that all of the parameters to re.sub() are the same type -- str or unicode .您应该确保re.sub()所有参数都是相同的类型—— strunicode Try this:尝试这个:

# encoding: utf-8

import re
s = u'アガサ・クリスティー 奥さまは名探偵 ~パディントン発4時50分~(字幕版)'
print re.sub(ur'\(.+$', u'', s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM