Python中re正则表达式的问题
黄舟
黄舟 2017-04-17 14:40:14
0
2
397
str1 = 'universe  ['juːnɪvɜːs]'

请问下各位,怎么用正则表达式匹配 ['juːnɪvɜːs]

使之替换成空,最后的结果是:str1 = 'universe'

恳请各位不吝赐教,不胜感激。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
Ty80

The answer above needs to be slightly modified. Regular Chinese square brackets [ ] themselves have grammatical meaning, so they need to be escaped. Just change it to this.

pythonimport re
str1 = re.sub(r'\[.*\]', '', "universe ['juːnɪvɜːs]")

The official description of the python re module is as follows:

https://docs.python.org/2/library/re.html

Sometimes you forget to check the regular rules, they are quite detailed.

迷茫
import re
str1 = re.sub(r'\[.*\]', '', "universe  ['juːnɪvɜːs]")

Segmentfaults seems to automatically [escape to [, which forces me to use code blocks.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!