May-09-2024, 06:26 AM
It is important to know that re uses its own parser to look at string expressions. Python passes a string or a raw string to re and re takes over.
Try this:
Whichever pattern you feed to re, re finds \n
. is a special character within re. \. is not a special character like \n
. represents, in re, any character except newline. If you feed '.' to re, you will get back all characters, but if you escape . as \. reu will just find the dot.
To quote the re docs:
Try this:
Quote:pattern = '\n'
pattern
'\n'
pattern = r'\n'
pattern
'\\n'
Whichever pattern you feed to re, re finds \n
. is a special character within re. \. is not a special character like \n
. represents, in re, any character except newline. If you feed '.' to re, you will get back all characters, but if you escape . as \. reu will just find the dot.
To quote the re docs:
Quote:Raw String Notation
Raw string notation (r"text") keeps regular expressions sane. Without it, every backslash ('\') in a regular expression would have to be prefixed with another one to escape it. For example, the two following lines of code are functionally identical:
re.match(r"\W(.)\1\W", " ff ") re.match("\\W(.)\\1\\W", " ff ")When one wants to match a literal backslash, it must be escaped in the regular expression. With raw string notation, this means r"\\". Without raw string notation, one must use "\\\\", making the following lines of code functionally identical:
re.match(r"\\", r"\\") re.match("\\\\", r"\\")