Python regex for int with at least 4 digits

后端 未结 3 1646
走了就别回头了
走了就别回头了 2020-12-20 19:29

I am just learning regex and I\'m a bit confused here. I\'ve got a string from which I want to extract an int with at least 4 digits and at most 7 digits. I tried it as foll

相关标签:
3条回答
  • 2020-12-20 19:57

    .match will only match if the string starts with the pattern. Use .search.

    0 讨论(0)
  • 2020-12-20 20:02

    @ExplosionPills is correct, but there would still be two problems with your regex.

    First, $ matches the end of the string. I'm guessing you'd like to be able to extract an int in the middle of the string as well, e.g. abcd123456efg789 to return 123456. To fix that, you want this:

    r"[0-9]{4,7}(?![0-9])"
                ^^^^^^^^^
    

    The added portion is a negative lookahead assertion, meaning, "...not followed by any more numbers." Let me simplify that by the use of \d though:

    r"\d{4,7}(?!\d)"
    

    That's better. Now, the second problem. You have no constraint on the left side of your regex, so given a string like abcd123efg123456789, you'd actually match 3456789. So, you need a negative lookbehind assertion as well:

    r"(?<!\d)\d{4,7}(?!\d)"
    
    0 讨论(0)
  • 2020-12-20 20:14

    You can also use:

    re.findall(r"[0-9]{4,7}", teststring)
    

    Which will return a list of all substrings that match your regex, in your case ['123456']

    If you're interested in just the first matched substring, then you can write this as:

    next(iter(re.findall(r"[0-9]{4,7}", teststring)), None)
    
    0 讨论(0)
提交回复
热议问题