在python中拆分多个字符的字符串

时间:2014-03-31 03:55:24

标签: python regex string split

我正在尝试在python中的多个字符上拆分字符串,就像我在Java中这样做:

private static final String SPECIAL_CHARACTERS_REGEX = "[ :;'?=()!\\[\\]-]+|(?<=\\d)(?=\\D)";
String rawMessage = "let's meet tomorrow at 9:30p? 7-8pm? i=you go (no Go!) [to do !]";
String[] tokens = rawMessage.split(SPECIAL_CHARACTERS_REGEX);
System.out.println(Arrays.toString(tokens));

以下是具有正确输出的工作演示:Working Demo

我试图在python中做同样的事情,但是当我这样做时,如果我只是在正则表达式中添加“单引号”字符,它就不会被标记化。如何从python创建与上面的Java程序相同的结果解析结果?

此:

import re
tokens = re.split(' \.', line);
print tokens

对于行:

"let's meet tomorrow at 9:30p? 7-8pm? i=you go (no Go!) [to do !]"

给出:

["let's meet tomorrow at 9:30p? 7-8pm? i=you go (no Go!) [to do !]";]

当我这样做时:

[let, s, meet, tomorrow, at, 9, 30, p, 7, 8, pm, i, you, go, no, Go, to, do]

4 个答案:

答案 0 :(得分:2)

这是找到而不是拆分的替代方案:

>>> s = "let's meet tomorrow at 9:30p? 7-8pm? i=you go (no Go!) [to do !]"
>>> re.findall(r'\d+|[A-Za-z]+', s)
['let', 's', 'meet', 'tomorrow', 'at', '9', '30', 'p', '7', '8', 'pm', 'i', 'you', 'go', 'no', 'Go', 'to', 'do']

如果可以将字母和数字放在一起,请使用'[0-9A-Za-z]+'。对于字母,数字和下划线,请使用r'\w+'

答案 1 :(得分:1)

使用您在Java中使用的相同正则表达式:

line = "let's meet tomorrow at 9:30p? 7-8pm? i=you go (no Go!) [to do !]"
tokens = re.split("[ :;'?=()!\\[\\]-]+|(?<=\\d)(?=\\D)", line)
tokens = [token for token in tokens if len(token) != 0] # remove empty strings!
print(tokens)
# ['let', 's', 'meet', 'tomorrow', 'at', '9', '30p', '7', '8pm', 'i', 'you', 'go', 'no', 'Go', 'to', 'do']

答案 2 :(得分:0)

使用以下代码

>>> chars = "[:;'?=()!\-]+<" #Characters to remove
>>> sentence = "let's meet tomorrow at 9:30p? 7-8pm? i=you go (no Go!) [to do !]" #Sentence
>>> for k in sentence: #Loops over everything in the sentence
...     if k in chars: #Checks if the variable is one we want to remove
...             sentence = sentence.replace(k, ' ') #If it is, it replaces it
...
>>> sentence = sentence.replace('p', ' p').replace('pm', ' pm').split() #Adds a space before the 'p' and the 'pm', and then splits it the way we want to
>>> sentence
['let', 's', 'meet', 'tomorrow', 'at', '9', '30', 'p', '7', '8', 'pm', 'i', 'you', 'go', 'no', 'Go', 'to', 'do']

如果您想使用regex

line = "let's meet tomorrow at 9:30p? 7-8pm? i=you go (no Go!) [to do !]"
tokens = re.split("[ :;'?=()!\\[\\]-]+|(?<=\\d)(?=\\D)", line)
tokens = [token for token in tokens if len(token) != 0]
tokens = tokens.replace('p', ' p').replace('pm', ' pm').split()
print(tokens)
#['let', 's', 'meet', 'tomorrow', 'at', '9', '30', 'p', '7', '8', 'pm', 'i', 'you', 'go', 'no', 'Go', 'to', 'do']

答案 3 :(得分:0)

Java中的分裂正则表达式应该在Python中使用相同的功能 它可能是一个错误。混乱可能是重叠 在\D[ :;'?=()!\[\]-]之间,以及它如何处理(bug~)。

您可以先将(?<=\d)(?=\D)放在首位,然后再尝试解决问题 必须被胁迫才能做到这一点。

这个正则表达式迫使它这样做。这是一种解决方法吗? 我不知道,没有python可以测试。但是,它适用于Perl。

强制正则表达式 -

 #  (?<=\d)(?:[ :;'?=()!\[\]-]+|(?=\D))|(?<!\d|[ :;'?=()!\[\]-])[ :;'?=()!\[\]-]+

    (?<= \d )
    (?:
         [ :;'?=()!\[\]-]+ 
      |  (?= \D )
    )
 |  
    (?<! \d | [ :;'?=()!\[\]-] )
    [ :;'?=()!\[\]-]+