Regex: match only once

前端 未结 1 700
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 07:40

I\'ve got a string with multiple ip addresses together with some random stuff. For example like this one:

21/Jun/2018:01:15:38 +0000    188.79.169.152    157.52.         


        
相关标签:
1条回答
  • You can use re.match which matches a ptrn from the beginning of a string So just by adding a .* to the start of your pattern, we can match everything from the beginning of the string to the first ip address

    >>> import re
    >>> s = "21/Jun/2018:01:15:38 +0000    188.79.169.152    157.52.69.50    443    -    -    GET / 157.52.69.30 157.52.69.10"
    >>> 
    >>> ptrn = r'.*?([0-9]+(?:\.[0-9]+){3})'
    >>> re.match(ptrn, s).groups()[0]
    '188.79.169.152'
    
    0 讨论(0)
提交回复
热议问题