Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据python中的标点符号分离(或断开)字符串_Python_Regex_String_Raw Input_Punctuation - Fatal编程技术网

根据python中的标点符号分离(或断开)字符串

根据python中的标点符号分离(或断开)字符串,python,regex,string,raw-input,punctuation,Python,Regex,String,Raw Input,Punctuation,有没有办法根据标点符号来打断字符串 #!/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

有没有办法根据标点符号来打断字符串

#!/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>
#/usr/bin/python
#要求用户以指定格式输入一行
myString=raw\u input('输入您的字符串:\n例如:我是break医生,我在CA break,您可以通过+000000\n'与我联系)
#“break”是一个标点词
期望输出像

String1:我是医生

String2:我留在CA


String2:您可以通过+000000联系我

您也可以对字符串使用
split
方法,该方法将根据分割分隔符返回所有令牌的列表

>>> a="test break testagain break again!"
>>> a.split(" break ")
['test ', ' testagain ', ' again!']

一个基于
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']

它成功了,谢谢你的快速回答!这也将拆分
“不可拆分”
,使用
拆分(“断开”)