why this regex cannot find the result

后端 未结 3 1689
梦谈多话
梦谈多话 2021-01-21 14:56

I have a python code like below: My question is why the matched variable is [\' \']? (I used the regex in regexpal.com, it can find the right result |Name=A. Johnson | there)

相关标签:
3条回答
  • 2021-01-21 15:53
    matched = re.findall("\|?\s*[nN]ame\s*=([a-zA-Z\.\s]+)\|?",a,re.I)
    print matched
    

    output:

    ['A. Johnson ']
    
    0 讨论(0)
  • 2021-01-21 15:55

    Looks to be how it's handling grouping. As a simpler example, look at the difference between the output of the following lines of code:

    re.findall("c(a)*t", "hi caaat hi")
    re.findall("c(a*)t", "hi caaat hi")
    

    It looks like the code you want would be more like:

    re.findall("\|\s*name\s*=([^\|\}]*)", a, re.I)
    
    0 讨论(0)
  • 2021-01-21 15:58

    You'll want (.*?), not (.)*?—the latter (what you have) will only capture a single character, even if it consumes more than a single one. A capture group will only be returned once even if the group itself has a repeat; so the latter captures a single character (.) despite its repeat.

    If you move the repeat into the capture group with (.*?), you'll get more than a single character in the return.

    0 讨论(0)
提交回复
热议问题