繁体   English   中英

使用预定义字符分隔字符串

[英]Separating a string using predefined characters

我输入的文字是“ Hi, my name is Will, And i am from Canada. I have 2 pets. One is a dog and the other is a Zebra. Ahoi! Thanks. Hi, my name is Will, And i am from Canada. I have 2 pets. One is a dog and the other is a Zebra. Ahoi! Thanks.

我想把这句话从. 和“!”,我该怎么做。 我也想知道句子是从什么字符中分离出来的。

例如,结果应为:

范例1:

Hi, my name is Will, And i am from Canada || The sentence was split with .

范例2:

Ahoi! || The sentence was split with !

我怎样才能做到这一点? 到目前为止我的工作:

print (text.split('.')) -这只会打断句子. ,而且我无法确定它曾经用来拆分什么字符。

您可以使用re.split()

re.split('[.!]', text)

这会分割[...]字符类中的任何字符:

>>> import re
>>> text = 'Hi, my name is Will, And i am from Canada. I have 2 pets. One is a dog and the other is a Zebra. Ahoi! Thanks.'
>>> re.split('[.!]', text)
['Hi, my name is Will, And i am from Canada', ' I have 2 pets', ' One is a dog and the other is a Zebra', ' Ahoi', ' Thanks', '']

您可以对拆分表达式进行分组,以在输出的单独列表元素中包含字符:

>>> re.split('([.!])', text)
['Hi, my name is Will, And i am from Canada', '.', ' I have 2 pets', '.', ' One is a dog and the other is a Zebra', '.', ' Ahoi', '!', ' Thanks', '.', '']

要使标点符号附加在句子上,请使用re.findall()代替:

>>> re.findall('[^.!]+?[.!]', text)
['Hi, my name is Will, And i am from Canada.', ' I have 2 pets.', ' One is a dog and the other is a Zebra.', ' Ahoi!', ' Thanks.']
>>> sp=re.split('(\.)|(!)','aaa.bbb!ccc!ddd.eee')
>>> sp
['aaa', '.', None, 'bbb', None, '!', 'ccc', None, '!', 'ddd', '.', None, 'eee']
>>> sp[::3] # the result
['aaa', 'bbb', 'ccc', 'ddd', 'eee']
>>> sp[1::3] # where matches `.`
['.', None, None, '.']
>>> sp[2::3] # where matches `!`
[None, '!', '!', None]

暂无
暂无

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

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