Python regular expressions - re.search() vs re.findall()

后端 未结 2 1016
感动是毒
感动是毒 2020-12-08 20:16

For school I\'m supposed to write a Python RE script that extracts IP addresses. The regular expression I\'m using seems to work with re.search() but not with <

2条回答
  •  青春惊慌失措
    2020-12-08 20:55

    findall returns a list of matches, and from the documentation:

    If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

    So, your previous expression had one group that matched 3 times in the string where the last match was 0.

    To fix your problem use: exp = "(?:\d{1,3}\.){3}\d{1,3}"; by using the non-grouping version, there is no returned groups so the match is returned in both cases.

提交回复
热议问题