【问题标题】:python, regex split and special characterpython,正则表达式拆分和特殊字符
【发布时间】:2010-10-13 10:57:28
【问题描述】:

如何使用空格作为分隔符正确拆分包含带有特殊字符的句子的字符串? 使用正则表达式拆分方法我无法获得所需的结果。

示例代码:

# -*- coding: utf-8 -*-
import re


s="La felicità è tutto" # "The happiness is everything" in italian
l=re.compile("(\W)").split(s)

print " s> "+s
print " wordlist> "+str(l)
for i in l:
    print " word> "+i

输出是:

 s> La felicità è tutto
 wordlist> ['La', ' ', 'felicit', '\xc3', '', '\xa0', '', ' ', '', '\xc3', '', '\xa8', '', ' ', 'tutto']
 word> La
 word>  
 word> felicit
 word> Ã
 word> 
 word> ?
 word> 
 word>  
 word> 
 word> Ã
 word> 
 word> ?
 word> 
 word>  
 word> tutto

当我正在寻找类似的输出时:

 s> La felicità è tutto
 wordlist> ['La', ' ', 'felicità', ' ', 'è', ' ', 'tutto']
 word> La
 word>  
 word> felicità
 word>  
 word> è
 word>  
 word> tutto

需要注意的是 s 是从另一个方法返回的字符串,所以我不能像这样强制编码

s=u"La felicità è tutto"

关于 Unicode 和 reg-ex 的官方 python 文档,我还没有找到令人满意的解释。

谢谢。

亚历山德罗

【问题讨论】:

  • 您正在拆分非单词字符,其中不仅包括空格,还包括(显然)重音字符。

标签: python regex unicode split


【解决方案1】:

使用 unicode 正则表达式将起作用,前提是您给它一个 unicode 字符串开头(您在提供的示例中没有)。试试这个:

s=u"La felicità è tutto" # "The happiness is everything" in italian
l=re.compile("(\W)",re.UNICODE).split(s)

print " s> "+s
print " wordlist> "+str(l)
for i in l:
    print " word> "+i

结果:

 s> La felicità è tutto
 wordlist> [u'La', u' ', u'felicit\xe0', u' ', u'\xe8', u' ', u'tutto']
 word> La
 word>  
 word> felicità
 word>  
 word> è
 word>  
 word> tutto

您的字符串s 被创建为str 类型,并且可能采用不同于unicode 的utf-8 编码。

【讨论】:

    【解决方案2】:

    嗯, 在对 Andrew Hare 答案进行进一步测试后,我发现 ()[]- 等字符不再被视为分隔符,而我想用字母数字值集合组成的单词拆分句子(保留所有分隔符) set 最终用重音字符扩展(即,在 unicode 中标记为字母数字的所有内容)。 因此,kgiannakakis 的解决方案更正确,但它错过了将字符串 s 转换为 unicode 格式。

    以第一个例子的扩展为例:

    # -*- coding: utf-8 -*-
    import re
    s="(La felicità è tutto)"#no explicit unicode given string (UTF8)
    l=re.compile("([\W])",re.UNICODE).split(unicode(s,'utf-8'))#split on s converted to unicode from utf8
    
    print " string> "+s
    print " wordlist> "+str(l)
    for i in l:
        print " word> "+i
    

    现在的输出是:

     string> (La felicità è tutto)
     wordlist> [u'', u'(', u'La', u' ', u'felicit\xe0', u' ', u'\xe8', u' ', u'tutto', u')', u'']
     word> 
     word> (
     word> La
     word>  
     word> felicità
     word>  
     word> è
     word>  
     word> tutto
     word> )
     word> 
    

    这正是我想要的。

    干杯:)

    亚历山德罗

    【讨论】:

      【解决方案3】:

      我认为在这种情况下使用正则表达式有点过头了。如果您只想将字符串拆分为空白字符,我建议您在字符串上使用split 方法

      s = 'La felicità è tutto'
      words = s.split()
      

      【讨论】:

      • 我的目的是在列表中保留空格,因此字符串拆分对此没有帮助,因为它会删除空格并且不能完全配置为正则表达式拆分。
      • @alexroat:你究竟为什么需要这些空间?您知道每个单词(列表项)之间发生的情况,您不能让您的算法在必要时将它们添加回来吗?
      【解决方案4】:

      尝试为正则表达式定义编码:

      l=re.compile("\W", re.UNICODE).split(s)
      

      【讨论】:

      • 它不起作用,我已经尝试过了......但是Andrew Hare的解决方案效果很好。
      • 是的,但行为就像字符串拆分(它删除空格),我想维护它们。但是 re.UNICODE 会改变一些字符的编码。
      【解决方案5】:

      您的正则表达式应该是(\s) 而不是(\W),如下所示:

      l = re.compile("(\s)").split(s)
      

      上面的代码将为您提供您所要求的确切输出。但是下面这行更有意义:

      l = re.compile("\s").split(s)
      

      它在空白字符上分割并且不会给你所有的空格作为匹配。不过你可能需要它们,所以我发布了两个答案。

      【讨论】:

      • 谢谢,它适用于单个单词的打印。为什么列表的打印包含 unicode 十六进制代码而不是解码的字符?
      • 这意味着输出是有效的 Python 代码,您可以将其复制并粘贴回去......并且由于您可能在非 Unicode 环境中工作,因此它以最便携的方式输出.
      • 谢谢安德鲁。你完全回答了我所有的疑问。
      • 完成了,但我还有一个问题:为什么不将 \s ()[]- 等作为分隔符?
      • 它们是正则表达式语法使用的字符。如果你想在 ] 出现时分隔字符串,你应该用 ] 转义它(就像你用正则表达式进行模式匹配一​​样)。 Benvenuto su stackoverflow :)
      猜你喜欢
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多