0

句読点に基づいて文字列を分割する方法はありますか

#!/usr/bin/python

#Asking user to Enter a line in specified format
myString=raw_input('Enter your String:\nFor Example:I am doctor break I stays in CA break   you can contact me on +000000\n')
# 'break' is punctuation word 
<my code which breaks the user input based on break word and returns output in different lists>

次のような出力が期待されます

String1:I am doctor

String2:I stays in CA

String2:you can contact me on +000000

4

2 に答える 2

1

ベースのregexソリューションであり、これは末尾と先頭の空白も処理します。

>>> import re
>>> text = "I am doctor break I stays in CA break   you can contact me on +000000\n"
>>> re.split(r'\s+break\s+', text)
['I am doctor', 'I stays in CA', 'you can contact me on +000000\n']
于 2013-10-11T07:29:45.443 に答える