Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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 - Fatal编程技术网

Python 按找到的第一个子字符串拆分字符串

Python 按找到的第一个子字符串拆分字符串,python,regex,Python,Regex,我想在一个句子第一次出现的时候,就把它分成几个词。让我举例说明: message = 'I wish to check my python code for errors to run the program properly with fluency' 我希望通过第一次出现的for/to/with来拆分上述消息,因此上述消息的结果将是检查我的python代码是否有错误,以流畅地正确运行程序 我还希望包括我在句子中使用的单词,因此我的最终结果是: 检查我的python代码是否有错误以流畅地正确

我想在一个句子第一次出现的时候,就把它分成几个词。让我举例说明:

message = 'I wish to check my python code for errors to run the program properly with fluency'
我希望通过第一次出现的
for/to/with
来拆分上述消息,因此上述消息的结果将是
检查我的python代码是否有错误,以流畅地正确运行程序

我还希望包括我在句子中使用的单词,因此我的最终结果是:
检查我的python代码是否有错误以流畅地正确运行程序

我的代码不起作用:

import re
message = 'I wish to check my python code for errors to run the program properly with fluency'
result = message.split(r"for|to|with",1)[1]
print(result)

我能做什么呢?

我猜这个简单的表达式可能就是这么做的

.*?(\b(?:to|for|with)\b.*)
re.match
可能是这五种方法中最快的一种:

使用
re.findall进行测试
使用
re.sub进行测试
使用
re.finditer进行测试
使用
re.match进行测试
使用
re.search进行测试
如果您希望进一步探索或修改该表达式,将在的右上面板中解释该表达式。在中,您可以查看它如何与一些示例输入匹配(如果您愿意)

message = 'I wish to check my python code for errors to run the program properly with fluency'
array = message.split(' ')
number = 0
message_new = ''
for i in range(len(array)):
    if array[i] == 'to' or array[i] == 'for':
        number=i
        break
for j in range(number,len(array)):
    message_new += array[j] + ' '
print(message_new) 
输出:

to check my python code for errors to run the program properly with fluency 

首先,您可以找到
for
to
with
的所有实例,按所需值拆分,然后拼接并重新连接:

import re
message = 'I wish to check my python code for errors to run the program properly with fluency'
vals, [_, *s] = re.findall(r"\bfor\b|\bto\b|\bwith\b", message), re.split(r"\bfor\b|\bto\b|\bwith\b", message)
result = ''.join('{} {}'.format(a, re.sub("^\s+", "", b)) for a, b in zip(vals, s))
输出:

'to check my python code for errors to run the program properly with fluency'

split
不将正则表达式作为参数(可能您考虑的是Perl)

以下内容符合您的要求:

import re
message = 'I wish to check my python code for errors to run the program properly with fluency'
result = re.search(r'\b(for|to|with)\b', message)
print message[result.start(1):]

这不使用替换、重新连接或循环,而只是简单地搜索所需字符串并使用该字符串的位置结果。

该问题已在中得到回答: 但它只适用于一个特定的分隔符,对于多个分隔符,您必须首先找出哪一个先出现,可以在此处找到:
你从第一个猜测开始,我没有太多的想象力,所以让我们称它为bestDelimiter=firstDelimiter,找出它第一次出现的位置,将位置保存到bestPosition=第一次出现的位置,继续找出其余分隔符的位置,每次在当前bestPosition之前找到一个分隔符时,您都会更新变量bestDelimiter和bestPosition,最后首先出现的将是bestDelimiter,然后继续应用所需的操作,使用bestDelimiter

替换整个字符串,这样做效率很低-在我的计算机上,看起来比使用search()查找第一个结果慢三倍左右。
message = 'I wish to check my python code for errors to run the program properly with fluency'
array = message.split(' ')
number = 0
message_new = ''
for i in range(len(array)):
    if array[i] == 'to' or array[i] == 'for':
        number=i
        break
for j in range(number,len(array)):
    message_new += array[j] + ' '
print(message_new) 
to check my python code for errors to run the program properly with fluency 
import re
message = 'I wish to check my python code for errors to run the program properly with fluency'
vals, [_, *s] = re.findall(r"\bfor\b|\bto\b|\bwith\b", message), re.split(r"\bfor\b|\bto\b|\bwith\b", message)
result = ''.join('{} {}'.format(a, re.sub("^\s+", "", b)) for a, b in zip(vals, s))
'to check my python code for errors to run the program properly with fluency'
import re
message = 'I wish to check my python code for errors to run the program properly with fluency'
result = re.search(r'\b(for|to|with)\b', message)
print message[result.start(1):]