Skip to content Skip to sidebar Skip to footer

Regex To Get Seven Numbers In A Row?

Just wondering what the best regex to get seven and only seven numbers in a row is? Is there a way to use [0-9] seven times succinctly? Or should I just use a few ???? The seven nu

Solution 1:

Don't use a regular expression at all. Use an HTML parser, like BeautifulSoup:

from urllib2 import urlopen, Request
from bs4 import BeautifulSoup

resp = urlopen(Request('https://en.wikipedia.org/wiki/Anniston_City_Schools',
                       headers={'User-Agent': 'Stack Overflow'}))
soup = BeautifulSoup(resp.read())

table = soup.find('table', class_='infobox')

for row intable.find_all('tr'):
    if'NCES'in row.th.text:
        nces = row.td.a.text
        print nces
        break

This loads the URL data, finds the "infobox" table, then the row with the NCES entry.

There are 12 exactly-7-digit numbers in the HTML source, but the above code extracts the correct number in one go.

Solution 2:

This looks for 7 numbers, then makes sure the next characters isn't another number

\b[0-9]{7}(?![0-9])

Regular expression visualization

If you are fine with matching spaces around the entire 7 digits, this is okay too

\b[0-9]{7}\b

Regular expression visualization


If you want to match Asad's example NCSD Code:1234567 This should work

(?<![0-9])[0-9]{7}(?![0-9])

Regular expression visualization

Solution 3:

You can use this:

(?<=^|[^0-9])[0-9]{7}(?=$|[^0-9])

It will match 7 digits only, no more, no less.

Or using negative lookarounds...

(?<![0-9])[0-9]{7}(?![0-9])

Solution 4:

I'd go for

[^0-9]([0-9]{7})[^0-9]

Regular expression visualization

Edit live on Debuggex

Solution 5:

You can do:

'\s(\d{7})\s'

Using Python re module:

re.findall('\s(\d{7})\s',s)

Testing with:

s = 'abc 1 abc 22 abc 333 abcd 666666 ab7777777bc 7777777 abc 88888888'

gives:

#['7777777']

Post a Comment for "Regex To Get Seven Numbers In A Row?"