Python:使用正则表达式从另一个字符串中提取子字符串(如果存在)

Python:使用正则表达式从另一个字符串中提取子字符串(如果存在),python,regex,Python,Regex,我想使用正则表达式从url提取一个值(如果存在) 我的字符串: string = "utm_source=google&utm_campaign=replay&utm_medium=display&ctm_account=4&ctm_country=fr&ctm_bu=b2c&ctm_adchannel=im&esl-k=gdn|nd|c427558773026|m|k|pwww.ldpeople.com|t|dm|a100313

我想使用正则表达式从url提取一个值(如果存在)

我的字符串:

string = "utm_source=google&utm_campaign=replay&utm_medium=display&ctm_account=4&ctm_country=fr&ctm_bu=b2c&ctm_adchannel=im&esl-k=gdn|nd|c427558773026|m|k|pwww.ldpeople.com|t|dm|a100313514420|g9711440090"
从这个字符串中,我想提取:c427558773026,要提取的值将始终以c开头,并且具有以下模式| c*|

import re
pattern = re.compile('|c\w|')
pattern.findall(string)
在我的例子中,结果是无。我使用的是python 2.7,您可以使用lookarounds在左右两侧断言一个管道(不是它被转义)
\\124;
,并匹配一个
c
字符,后跟1+位
\d+

(?<=\|)c\d+(?=\|)

您的方法的问题是
|
是or,必须对其进行转义以匹配文字字符。此外,您可以使用“向前看/向后看”来确保
|
正在封装字符串,而不是使用
findall

下面是一段代码片段,可以解决此问题:

>>重新导入
>>>string=“utm|u source=google&……&esl-k=gdn | nd | c427558773026 | m | k |…”

>>>pattern=re.compile(“(?Try
)(?在匹配项不重叠的情况下,不需要使用
re.findall
进行查找。
re.findall(r'\\\(c\d+)\\\”,text)
会更快。@WiktorStribiżew您在这方面是对的。并不是说它在示例数据中,而是它将在
|c427558773026 | c427558773026 |
中匹配一次。很好,那么,这是一种重叠匹配的情况,您可以使用
r'\\\\\(c\d+(=\\\\\)“
,不需要不经处理的后顾之忧。@WiktorStribiżew Nice,谢谢!这就是为什么你是大师:-)这是在原始答案后8分钟重新发布的。@WiktorStribiżew当我写我的答案时,他的答案是不完整的,缺乏完整的代码解决方案。我对高射炮和否决票很感兴趣。答案是完整的,有解释,并且包含一个现成的解决方案。
import re
string = "utm_source=google&utm_campaign=replay&utm_medium=display&ctm_account=4&ctm_country=fr&ctm_bu=b2c&ctm_adchannel=im&esl-k=gdn|nd|c427558773026|m|k|pwww.ldpeople.com|t|dm|a100313514420|g9711440090"
print(re.findall(r"(?<=\|)c\d+(?=\|)", string))
\|(c\d+)(?=\|)