in regex is a metacharacter, it is used to match any character. To match a literal dot in a raw Python string ( r"" or r'' ), you need to escape it, so r"\." Unless the regular expression is stored inside a regular python string, in which case you need to use a double \ ( \\ ) instead.
Yes, the dot regex matches whitespace characters when using Python's re module. What is this? The dot matches all characters in the string --including whitespaces. You can see that there are many whitespace characters ' ' among the matched characters.
It's a negative lookahead, which means that for the expression to match, the part within (?!...) must not match. In this case the regex matches http:// only when it is not followed by the current host name (roughly, see Thilo's comment). Follow this answer to receive notifications.
(dot) metacharacter, and can match any single character (letter, digit, whitespace, everything). You may notice that this actually overrides the matching of the period character, so in order to specifically match a period, you need to escape the dot by using a slash \.
A .
in regex is a metacharacter, it is used to match any character. To match a literal dot in a raw Python string (r""
or r''
), you need to escape it, so r"\."
In your regex you need to escape the dot "\."
or use it inside a character class "[.]"
, as it is a meta-character in regex, which matches any character.
Also, you need \w+
instead of \w
to match one or more word characters.
Now, if you want the test.this
content, then split
is not what you need. split
will split your string around the test.this
. For example:
>>> re.split(r"\b\w+\.\w+@", s)
['blah blah blah ', 'gmail.com blah blah']
You can use re.findall
:
>>> re.findall(r'\w+[.]\w+(?=@)', s) # look ahead
['test.this']
>>> re.findall(r'(\w+[.]\w+)@', s) # capture group
['test.this']
"In the default mode, Dot (.) matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline." (python Doc)
So, if you want to evaluate dot literaly, I think you should put it in square brackets:
>>> p = re.compile(r'\b(\w+[.]\w+)')
>>> resp = p.search("blah blah blah [email protected] blah blah")
>>> resp.group()
'test.this'
to escape non-alphanumeric characters of string variables, including dots, you could use re.escape
:
import re
expression = 'whatever.v1.dfc'
escaped_expression = re.escape(expression)
print(escaped_expression)
output:
whatever\.v1\.dfc
you can use the escaped expression to find/match the string literally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With