-1

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-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\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-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\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-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\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-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\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-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\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-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
    """
    , re.X)
Vaibhav Vishal
  • 6,576
  • 7
  • 27
  • 48
  • 1
    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 Jul 24 '18 at 10:13
  • 4
    Add [`re.X`](https://docs.python.org/2/library/re.html#re.X) as the second argument and escape `#` (and literal whitespaces if you have any or are going to add any). – Wiktor Stribiżew Jul 24 '18 at 10:13
  • 1
    FYI, https://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 Jul 24 '18 at 10:17
  • 1
    The regex doesn't look particularly good. Where did you get it from and what is it supposed to accomplish? – tripleee Jul 24 '18 at 10:24

1 Answers1

0

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 [...]

Guillaume
  • 2,325
  • 2
  • 22
  • 40