找到你要的答案

Q:Python 3: splitting by 3rd delimiter Code Outcome Desired outcome Notes

Q:Python 3:第三分隔符分割 代码 结果 期望的结果 笔记

I need to split the data by the third delimiter while including that delimiter in the output.

Code

text = 'sitting on a couch sitting on a chair sitting on a bench'

splitText = text.split('sitting')[1]

print(splitText)

Outcome

on a couch sitting on a chair sitting on a bench

Desired outcome

sitting on a bench

Notes

  • SPLIT FUNCTION DOES NOT INCLUDE 'sitting' IN OUTCOME WHEN USING IT AS A DELIMITER

  • 'sitting' MUST BE INCLUDED IN THE OUTCOME

我需要把数据由第三个分隔符,包括在输出分隔符。

代码

text = 'sitting 坐在长椅上的长椅上'

splitText = text.split('sitting')[1]

print(splitText)

结果

坐在长椅上的长椅上

期望的结果

坐在长凳上

笔记

  • 分裂的功能不包括“坐”在结局时使用它作为一个分隔符

  • “坐”必须包括在结果

answer1: 回答1:
'sitting' + text.split('sitting')[3]
'sitting' + text.split('sitting')[3]
answer2: 回答2:

You can simply split by space before sitting.

x="sitting on a couch sitting on a chair sitting on a bench"
print re.split(r"\s(?=\bsitting\b(?:(?!\bsitting\b).)*$)",x)[1]

Or split on 0 width assertion which is not there in re module but there in regex module.

import regex
x="sitting on a couch sitting on a chair sitting on a bench"
print regex.split(r"(?=sitting)",x,flags=regex.VERSION1)[3]

Or use findall.

x="sitting on a couch sitting on a chair sitting on a bench"
print re.findall(r"(sitting.*?(?=sitting|$))",x)[2]

你可以简单地坐在空间分割。

x="sitting 坐在长椅上的长椅上"
print re.split(r"\s(?=\bsitting\b(?:(?!\bsitting\b).)*$)",x)[1]

或分0宽断言这是不存在的但在regex模块模块。

imp或t regex
x="sitting 坐在长椅上的长椅上"
print regex.split(r"(?=sitting)",x,flags=regex.VERSION1)[3]

或使用FindAll。

x="sitting 坐在长椅上的长椅上"
print re.findall(r"(sitting.*?(?=sitting|$))",x)[2]
answer3: 回答3:

You can use following regex :

r'(sitting.*){2}'

This regex will match a text that is contain the word sitting 2 time.then you can split the text with re.split() :

>>> text = 'sitting on a couch sitting on a chair sitting on a bench'
>>> import re
>>> re.split(r'(sitting.*){2}',text)
['', 'sitting on a bench', '']

You can get the result with a generator expression and next :

>>> next(i for i in re.split(r'(sitting.*){2}',text) if i)
'sitting on a bench'

你可以用下面的正则表达式:

r'(sitting.*){2}'

这个正则表达式匹配的文本,包含单词坐2然后你可以重新分割的文本。split():

>>> text = 'sitting 坐在长椅上的长椅上'
>>> imp或t re
>>> re.split(r'(sitting.*){2}',text)
['', '坐在长凳上', '']

你可以用一个生成器表达式得到结果,然后:

>>> next(i f或 i in re.split(r'(sitting.*){2}',text) if i)
'坐在长凳上'
answer4: 回答4:
import re

text = 'sitting on a couch sitting on a chair sitting on a bench'
splitText = re.findall('sitting.*?(?= sitting|$)', text)

if len(splitText) >= 3:
    print(splitText[2])

Eric: "This splits by regexes rather than splitting by word index." - there was "regex" tag in question, it you want to use only indexes - you can use one following samples:

text = 'sitting on a couch sitting on a chair sitting on a bench'
delim = 'sitting'
text[text.find(delim, text.find(delim, text.find(delim)+1)+1):]

or

def X(text, delim, n, pos=0):
        idx = text.find(delim, pos)
        if idx >= 0 and n > 0:
                return X(text, delim, n-1, idx+1)
        if n > 0:
                return ""
        if idx > 0:
                return text[pos-1:idx]
        return text[pos-1:]


text = 'sitting on a couch sitting on a chair sitting on a bench'
print(X(text, 'sitting', 3))
imp或t re

text = 'sitting 坐在长椅上的长椅上'
splitText = re.findall('sitting.*?(?= sitting|$)', text)

if len(splitText) >= 3:
    print(splitText[2])

埃里克:“这将由正则表达式而不是词索引分裂。”有“regex”标签的问题,它只需要使用指标-你可以使用一个样品:

text = 'sitting 坐在长椅上的长椅上'
delim = 'sitting'
text[text.find(delim, text.find(delim, text.find(delim)+1)+1):]

def X(text, delim, n, pos=0):
        idx = text.find(delim, pos)
        if idx >= 0 and n > 0:
                return X(text, delim, n-1, idx+1)
        if n > 0:
                return ""
        if idx > 0:
                return text[pos-1:idx]
        return text[pos-1:]


text = 'sitting 坐在长椅上的长椅上'
print(X(text, 'sitting', 3))
regex  python-3.x