Regular expression to match a dot [duplicate]

2024-12-31 08:37:00
admin
原创
118
摘要:问题描述:I was wondering what the best way to match "test.this" from "blah blah blah test.this@gmail.com blah blah" is? Using Python.I've t...

问题描述:

I was wondering what the best way to match "test.this" from "blah blah blah test.this@gmail.com blah blah" is? Using Python.

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


解决方案 1:

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

解决方案 2:

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

解决方案 3:

"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'(w+[.]w+)')
>>> resp = p.search("blah blah blah test.this@gmail.com blah blah")
>>> resp.group()
'test.this'

解决方案 4:

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

Summary

These are NOT allowed.

'.'   # NOT a valid escape sequence in **regular** Python single-quoted strings
"."   # NOT a valid escape sequence in **regular** Python double-quoted strings

They'll cause a warning like this:

DeprecationWarning: invalid escape sequence .

All of these, however, ARE allowed and are equivalent:

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

# Use a SINGLE BACK-SLASH in Python _raw_ strings 
r'.'  # Python single-quoted **raw** string
r"."  # Python double-quoted **raw** string

Explanation

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 above.

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] How to fix "<string> DeprecationWarning: invalid escape sequence" in Python?

If you want to put a literal ` in a string you have to use \`

解决方案 5:

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.

解决方案 6:

So first you need to construct your regex string.
For example this one matches your need:

^.*?test.this.*?

Next you need to put it into Python code:

import re

input_string = "blah blah blah test.this@gmail.com blah blah"
regex_string = "^.*?\\btest\\b\\.\\bthis\\b.*?"
if re.search(regex_string, input_string):
    print("match :-)")
else:
    print("no match :-(")

To understand what the regex does, you can play with that on regex101.com.

解决方案 7:

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 test.this@gmail.com blah blah
blah blah blah test.this @gmail.com blah blah
blah blah blah test.this.this@gmail.com 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.


解决方案 8:

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]
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用