-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)
1
  • %ss are placeholders for the variables passed after % symbol, this is not a regex, just a format string. Commented Jun 27, 2020 at 10:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.