Regular expression to match a dot

PythonRegex

Python Problem Overview


Was wondering what the best way is to match "test.this" from "blah blah blah [email protected] blah blah" is? Using Python.

I've tried re.split(r"\b\w.\w@")

Python Solutions


Solution 1 - Python

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"\."

Solution 2 - Python

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']

Solution 3 - Python

> "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'

Solution 4 - Python

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.

Solution 5 - Python

Here is my add-on to the main answer by @Yuushi:

Keep in mind, the backslash (\) char itself must be escaped in Python if used inside of a regular string ('some string' or "some string") instead of a raw string (r'some string' or r"some string"). So, keep in mind the type of string you are using. To escape the dot or period (.) inside a regular expression in a regular python string, therefore, you must also escape the backslash by using a double backslash (\\), making the total escape sequence for the . in the regular expression this: \\., as shown in the examples below.

Therefore, these are NOT allowed. They'll cause a warning something like this: > DeprecationWarning: invalid escape sequence \.

'\.'   # NOT a valid escape sequence in Python
"\."   # NOT a valid escape sequence in Python

And all of these ARE allowed and are equivalent:

# Use a DOUBLE BACK-SLASH in Python _regular_ strings
'\\.'  # Python regular string
"\\."  # Python regular string

# Use a SINGLE BACK-SLASH in Python _raw_ strings 
r'\.'  # Python raw string
r"\."  # Python raw string

References:

  1. MAIN AND OFFICIAL REFERENCE: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals enter image description here
  2. [answer by @Sean Hammond] https://stackoverflow.com/questions/52335970/how-to-fix-string-deprecationwarning-invalid-escape-sequence-in-python/52335971#52335971 > If you want to put a literal \ in a string you have to use \\

Solution 6 - Python

In javascript you have to use \\. to match a dot.

Example

"blah.tests.zibri.org".match('test\\..*')
null

and

"blah.test.zibri.org".match('test\\..*')
["test.zibri.org", index: 5, input: "blah.test.zibri.org", groups: undefined]

Solution 7 - Python

This expression,

(?<=\s|^)[^.\s]+\.[^.\s]+(?=@)

might also work OK for those specific types of input strings.

###Demo

###Test

import re

expression = r'(?<=^|\s)[^.\s]+\.[^.\s]+(?=@)'
string = '''
blah blah blah [email protected] blah blah
blah blah blah test.this @gmail.com blah blah
blah blah blah [email protected] blah blah
'''

matches = re.findall(expression, string)

print(matches)

###Output

['test.this']

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser179169View Question on Stackoverflow
Solution 1 - PythonYuushiView Answer on Stackoverflow
Solution 2 - PythonRohit JainView Answer on Stackoverflow
Solution 3 - PythonStackUserView Answer on Stackoverflow
Solution 4 - PythonAli Abul HawaView Answer on Stackoverflow
Solution 5 - PythonGabriel StaplesView Answer on Stackoverflow
Solution 6 - PythonZibriView Answer on Stackoverflow
Solution 7 - PythonEmmaView Answer on Stackoverflow