-2

I'm trying to understand the following code related to complex regex.

I do not understand how the full_regex line operates? What is the use of the '%s' as well as the other % before the (regex1, regex2...)

Can someone please help with this?

regex1 = '(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})'
regex2 = '((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\S]*[+\s]\d{1,2}[,]{0,1}[+\s]\d{4})'
regex3 = '(\d{1,2}[+\s](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\S]*[+\s]\d{4})'
regex4 = '((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\S]*[+\s]\d{4})'
regex5 = '(\d{1,2}[/-][1|2]\d{3})'
regex6 = '([1|2]\d{3})'
full_regex = '(%s|%s|%s|%s|%s|%s)' %(regex1, regex2, regex3, regex4, regex5, regex6)
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397

1 Answers1

1

The expression

full_regex = '(%s|%s|%s|%s|%s|%s)' % (regex1, regex2, regex3, regex4, regex5, regex6)

just merges all of the other regexps into one big one that alternates between all of them; that's not regex syntax, it's just Python string interpolation.

AKX
  • 93,995
  • 11
  • 81
  • 98