Wednesday, July 1, 2020

Regular Expression - good practice and a good place to check


A good place to check if you regular expression does the work is https://regex101.com/

e.g. 




I have a list of company list extracted from some website's filter:

By using regular expression, I can easily extract the company number in two lines:


regex = re.compile(r'.*\((\d{5})\)')
company_value_list = [regex.match(item.text).group(1) for item in company_list
                      if re.match(regex, item.text) is not None]


So the result is a list of company numbers:

['39227', '65860', '39639', '68942', '68979', '68998', '68938', '62950'.....]

No comments:

Post a Comment