Python re.search () does not return full group match

import re ip6 = "1234:0678:0000:0000:00cd:0000:0000:0000" zeroes = re.search("(:?0000)+", ip6) print zeroes.group(0) :0000:0000 

I am trying to find the longest sequence of four zeros separated by colons. A line contains a sequence of three such groups, but only two groups are printed. Why?

EDIT: it prints: 0000: 0000 because the match is first on the line - but I thought regular expressions always looked for a long match?

+4
source share
3 answers

Answer updated to work in Python 2.6:

 p = re.compile('((:?0000)+)') longestword = "" for word in p.findall(ip6): if len(word[0])>len(longestword): longestword = word[0] print longestword 
+2
source

If you are not stuck in regexes, you can use itertools.groupby :

 from itertools import groupby ip6 = "1234:0678:0000:0000:00cd:0000:0000:0000" longest = 0 for section, elems in groupby(ip6.split(':')): if section == '0000': longest = len(list(elems)) print longest # Prints '3', the number of times '0000' repeats the most. # you could, of course, generate a string of 0000:... from this 

I am sure that this can be reduced to something more elegant, but I think it speaks to the point.

+2
source

I am using Python 2.7.3
How about using re.finditer ()

 $ uname -r 3.2.0-4-amd64 


 #!/usr/bin/env python import re ip6 = "1234:0678:0000:0000:00cd:0000:0000:0000" iters = re.finditer("(:?0000)+", ip6) for match in iters: print 'match.group() -> ',match.group() 
0
source

Source: https://habr.com/ru/post/1386761/


All Articles