如何获取关键字后面的字符串

2024-06-28 07:58:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想得到一个特定关键字后的字符串。你知道吗

例如:

import re
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search

abc = "<StephenCurry Pro='ThreepointShooter'>MVP1times</StephenCurry>"
if findWholeWord("SeedNumber")(abc):
    dddd = re.search('(?<=ThreepointShooter)(.\w+)', abc)
    mvp = dddd.gorup()
    print (mvp)

    print ("found")
else:
    print ("not found")

我希望结果是'MVP1times'。你知道吗

有没有更好的方法来查找关键字后面的特定字符串?结果可能是一个字符串,数字,甚至像上面的结果一样混合。你知道吗

谢谢你的帮助!你知道吗


Tags: 字符串importresearchdef关键字abcprint
2条回答

您可以使用look arounds来获取由><包围的字符串(假设保持一致):

>>> s = "<StephenCurry Pro='ThreepointShooter'>MVP1times</StephenCurry>"

>>> re.search(r'(?<=\>)[^<]+(?=\<)', s).group(0)
'MVP1times'

您可以将正则表达式更改为:(?<=ThreepointShooter['|"]>)(.\w+)See it live on http://pythex.org/

我不知道你到底要做什么,但你甚至不需要在这里使用lookback表达式。你知道吗

相关问题 更多 >