Multi Line Triple Quotes raw string regex not working [duplicate]

Multi tool use
Multi Line Triple Quotes raw string regex not working [duplicate]
This question already has an answer here:
I have a regex for email in python like this, whole regex in single line and it works perfectly
emailRegEx = re.compile(
r"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])"""
)
But when I try to split it into multiple lines it won't match anything, eg:
emailRegEx = re.compile(
r"""
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])
"""
)
As shown above even if I put one newline in the string it won't match any emails and I get an empty list. I have tried many things changing indentation and stuff but as soon as there is a newline anywhere between the triple quotes the regex stops working.
I am using VS Code.
EDIT:
Thanks to everyone for the comments and answer, its working now, here is my working code:
emailRegEx = re.compile(
r"""
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])
"""
, re.X)
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Add
re.X
as the second argument and escape #
(and literal whitespaces if you have any or are going to add any).– Wiktor Stribiżew
6 hours ago
re.X
#
FYI, stackoverflow.com/a/10660443/6345404 if you want multi line string, the third code snippet in the answer might be useful for you.
– T.Nel
6 hours ago
The regex doesn't look particularly good. Where did you get it from and what is it supposed to accomplish?
– tripleee
5 hours ago
Thanks everyone, its working now. Sorry about duplicate question, I searched a lot but couldn't find the answer.
– Vaibhav Vishal
3 hours ago
1 Answer
1
https://docs.python.org/3/library/re.html#re.X is what you are looking for I think:
This flag allows you to write regular expressions that look nicer and
are more readable by allowing you to visually separate logical
sections of the pattern and add comments. Whitespace within the
pattern is ignored [...]
Thanks it works.
– Vaibhav Vishal
3 hours ago
The second regex starts and ends with a line break... And it's also indented, which means it contains a bunch of spaces...
– Aran-Fey
6 hours ago