【问题标题】:Find a specific string between two specific words in Python3在 Python3 中查找两个特定单词之间的特定字符串
【发布时间】:2021-11-13 10:24:27
【问题描述】:

我想将多项选择题的词干和选项分开。

例如:

import re
newstr = "1 which season do you like best after looking at these pictures A spring B summer C autumn D winter E none"
result = re.search('1(.*)A', newstr)
print(result)

但输出结果不是我的预期结果。

<re.Match object; span=(0, 65), match='1 which season do you like best after looking at >

我希望得到的结果是:

str1 = 'which season do you like best after looking at these pictures'
str2 = 'spring summer autumn winter none'

我怎样才能实现它?

谢谢

【问题讨论】:

  • (?&lt;=1)(.*)(?&lt;=A) 提问

标签: python-3.x string list search re


【解决方案1】:

re.split试试这个:

>>> _, str1, str2 = re.split("(?<=1)\s(.*)\s(?=A)", newstr)
>>> str2 = re.sub("[A-Z]\s", "", str2)
>>> str1
'which season do you like best after looking at these pictures'
>>> str2
'spring summer autumn winter none'
>>> 

【讨论】:

    猜你喜欢
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多