[Tutor] Server stuff

Clay Shirky clay at shirky.com
Sun Aug 24 21:55:25 EDT 2003


> 2. Is there an easy way to extract a sequence of at least 6 digits from a
> string. Can't rely on the digits to be in a word by themselves.

You want to search the string for the regex \d{6,}  (that is, 6 or more
digits), and then print the matchobject, like so:

strings = [ '1234spam', '123456eggs', 'spam1234567toast', '12345678' ]

for string in strings:
    match = re.search( "(\d{6,})", string )
    if match: print match.group(1)

This prints

123456
1234567
12345678




More information about the Tutor mailing list