Erik Ásland Erik Ásland - 1 year ago 117
Python Question

Using Python's regex .match() method to get the string before and after an underscore

I have the following code...

tablesInDataset = ["henry_jones_12345678", "henry_jones", "henry_jones_123"]

for table in tablesInDataset:
tableregex = re.compile("\d{8}")
tablespec = re.match(tableregex, table)

everythingbeforedigits =
digits =

My regex should only return the string if it contains 8 digits after an underscore. Once it returns the string, I want to use
to get two groups using the
method. The first group should contain a string will all of the characters before the digits and the second should contain a string with the 8 digits.

Could someone please help me figure out the correct regex to use to get the results I am looking for using

Answer Source
tableregex = re.compile("(.*)_(\d{8})")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download