Python替换具有某些变量组件的子字符串

Python替换具有某些变量组件的子字符串,python,split,Python,Split,我有以下字符串: Billy got score of 2 and Tommy got score of 3 >>> import re >>> s = "Billy got score of 2 and Tommy got score of 3" >>> re.split(r' score of \d+', s) ['Billy got', ' and Tommy got', ''] 我想在的分数上进行拆分,以便 ["Billy got

我有以下字符串:

Billy got score of 2 and Tommy got score of 3
>>> import re
>>> s = "Billy got score of 2 and Tommy got score of 3"
>>> re.split(r' score of \d+', s)
['Billy got', ' and Tommy got', '']
我想在的
分数上进行拆分,以便

["Billy got","Tommy got"]
如何在python中进行这样的拆分?我试过了

input.split("score of \d")
但这是行不通的。然而,如果我这样做

input.split("score of")
然后我得到

["Billy got "," 2 and Tommy got "," 3"]
更新:

谢谢你对原始帖子的回复。我有一个跟进


如果我想将
分数2
替换为
分数2$
,该怎么办?意思是每当我看到
分数时,只需在数字后添加一个字符
$
,您需要使用
re.split
并在前面的字符串旁边对数字进行拆分:

Billy got score of 2 and Tommy got score of 3
>>> import re
>>> s = "Billy got score of 2 and Tommy got score of 3"
>>> re.split(r' score of \d+', s)
['Billy got', ' and Tommy got', '']
您还可以使用列表进行清理:

>>> [i.strip() for i in re.split(r' score of \d+', s) if i]
['Billy got', 'and Tommy got']

您需要使用
re.split
并在前面字符串旁边的数字上拆分:

Billy got score of 2 and Tommy got score of 3
>>> import re
>>> s = "Billy got score of 2 and Tommy got score of 3"
>>> re.split(r' score of \d+', s)
['Billy got', ' and Tommy got', '']
您还可以使用列表进行清理:

>>> [i.strip() for i in re.split(r' score of \d+', s) if i]
['Billy got', 'and Tommy got']

这不起作用的原因是因为需要一个字符串作为模式:它不被解释为正则表达式

但是,您可以使用:

您还应该添加可选的
(?:and)?
以删除
组合符。此外,此答案使用
\d+
(带有
+
)以便正确解析多位数分数(如
“Tommy得分23”

在口译员中:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input="Billy got score of 2 and Tommy got score of 3"
>>> import re
>>> re.split(r'score of \d+(?:\s*and\s*)?',input)
['Billy got ', 'Tommy got ', '']

这不起作用的原因是因为需要一个字符串作为模式:它不被解释为正则表达式

但是,您可以使用:

您还应该添加可选的
(?:and)?
以删除
组合符。此外,此答案使用
\d+
(带有
+
)以便正确解析多位数分数(如
“Tommy得分23”

在口译员中:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input="Billy got score of 2 and Tommy got score of 3"
>>> import re
>>> re.split(r'score of \d+(?:\s*and\s*)?',input)
['Billy got ', 'Tommy got ', '']

此处使用正则表达式的说明
(.+?)分数[0-9]+

  • 分数为
    并后跟一些数字的任何内容进行匹配
  • (.+?)
    使用非贪婪搜索提取
    得分之前的任何内容
代码如下:

>>> import re
>>> sentence
'Billy got score of 2 and Tommy got score of 3'
>>> sentence.replace(' and ', ' ')
'Billy got score of 2 Tommy got score of 3'
>>> results = re.findall('(.+?) score of [0-9]+', sentence.replace(' and ', ' '))
>>> print results
['Billy got', ' Tommy got']

此处使用正则表达式的说明
(.+?)分数[0-9]+

  • 分数为
    并后跟一些数字的任何内容进行匹配
  • (.+?)
    使用非贪婪搜索提取
    得分之前的任何内容
代码如下:

>>> import re
>>> sentence
'Billy got score of 2 and Tommy got score of 3'
>>> sentence.replace(' and ', ' ')
'Billy got score of 2 Tommy got score of 3'
>>> results = re.findall('(.+?) score of [0-9]+', sentence.replace(' and ', ' '))
>>> print results
['Billy got', ' Tommy got']

查看是否要使用正则表达式进行拆分。查看是否要使用正则表达式进行拆分。您可能想要
[和]?
(方括号),对吗?@MSeifert:但是
[]
是一个字符组,不是可选模式。有一段时间没有使用正则表达式,但是
(和)
应在单独的子字符串中匹配
。你需要让它成为模式的一部分,比如
[(和)]
(这样行吗?@MSeifert:那是因为它是一个捕获组,如果你添加
?:
,你会使它成为一个非捕获组感谢你的回答。我能问一下最新情况吗?如果我想将
分数2
替换为
分数2$
,该怎么办?意思是每当我看到
的分数时,只要在你可能想要的
[和]?
(方括号)后面添加一个字符
$
,对吗?@MSeifert:但是
[]
是一个字符组,不是可选模式。很久没有使用regex了,但是
(和)
应在单独的子字符串中匹配
。你需要让它成为模式的一部分,比如
[(和)]
(这样行吗?@MSeifert:那是因为它是一个捕获组,如果你添加
?:
,你会使它成为一个非捕获组感谢你的回答。我能问一下最新情况吗?如果我想将
分数2
替换为
分数2$
,该怎么办?意思是每当我看到
分数时,只要在数字后面加一个字符
$