在perl
中,我可以执行以下操作,并使用空格填充标点符号:
s/([،;؛¿!"\])}»›”؟%٪°±©®।॥…])/ $1 /g;`
在Python
中,我试过了这个:
>>> p = u'،;؛¿!"\])}»›”؟%٪°±©®।॥…'
>>> text = u"this, is a sentence with weird» symbols… appearing everywhere¿"
>>> for i in p:
... text = text.replace(i, ' '+i+' ')
...
>>> text
u'this, is a sentence with weird \xbb symbols \u2026 appearing everywhere \xbf '
>>> print text
this, is a sentence with weird » symbols … appearing everywhere ¿
但是有没有办法使用某种占位符符号,例如$1
perl
python
我可以在$scope.clearHistory = function() {
$ionicHistory.nextViewOptions({
disableBack: true,
historyRoot: true
});
}
中使用1个正则表达式执行相同操作吗?
答案 0 :(得分:2)
$1
的Python版本是\1
,但您应该使用正则表达式替换而不是简单的字符串替换:
import re
p = ur'([،;؛¿!"\])}»›”؟%٪°±©®।॥…])'
text = u"this, is a sentence with weird» symbols… appearing everywhere¿"
print re.sub(p, ur' \1 ', text)
输出:
this , is a sentence with weird » symbols … appearing everywhere ¿
答案 1 :(得分:2)
您可以使用re.sub
,\1
作为占位符。
>>> p = u'،;؛¿!"\])}»›”؟%٪°±©®।॥…'
>>> text = u"this, is a sentence with weird» symbols… appearing everywhere¿"
>>> text = re.sub(u'([{}])'.format(p), r' \1 ', text)
>>> print text
this, is a sentence with weird » symbols … appearing everywhere ¿
答案 2 :(得分:0)
使用format
功能,并插入unicode
字符串:
p = u'،;؛¿!"\])}»›”؟%٪°±©®।॥…'
text = u"this, is a sentence with weird» symbols… appearing everywhere¿"
for i in p:
text = text.replace(i, u' {} '.format(i))
print(text)
<强>输出强>
this, is a sentence with weird » symbols … appearing everywhere ¿