代码之家  ›  专栏  ›  技术社区  ›  user366312

为什么grep和Notepad++的结果不同?[副本]

  •  -1
  • user366312  · 技术社区  · 4 年前

    想知道最好的匹配方式是什么 "test.this" 从…起 "blah blah blah test.this@gmail.com blah blah" 是使用Python。

    我试过了 re.split(r"\b\w.\w@")

    0 回复  |  直到 6 年前
        1
  •  229
  •   Gabriel Staples    5 年前

    A. . 在regex中,它是一个元字符,用于匹配任何字符。匹配原始Python字符串中的文字点( r"" r'' ),你需要逃离它,所以 r"\."

        2
  •  55
  •   wjandrea sebs    6 年前

    在正则表达式中,您需要 逃跑 "\." 或者在 字符类 "[.]" ,因为它是regex中的元字符,可以匹配任何字符。

    此外,您需要 \w+ 而不是 \w 以匹配一个或多个单词字符。


    现在,如果你想要 test.this 内容,然后 split 不是你需要的。 分裂 会把你的绳子绕在 测试.this 。例如:

    >>> re.split(r"\b\w+\.\w+@", s)
    ['blah blah blah ', 'gmail.com blah blah']
    

    您可以使用 re.findall :

    >>> re.findall(r'\w+[.]\w+(?=@)', s)   # look ahead
    ['test.this']
    >>> re.findall(r'(\w+[.]\w+)@', s)     # capture group
    ['test.this']
    
        3
  •  15
  •   StackUser    12 年前

    “在默认模式下,Dot(.)匹配除换行符之外的任何字符。如果指定了DOTALL标志,它将匹配包括换行符在内的任何字符”(python Doc)

    所以,如果你想直接评估dot,我认为你应该把它放在方括号里:

    >>> p = re.compile(r'\b(\w+[.]\w+)')
    >>> resp = p.search("blah blah blah test.this@gmail.com blah blah")
    >>> resp.group()
    'test.this'
    
        4
  •  3
  •   Gabriel Staples    4 年前

    这是我的附加组件 the main answer by @Yuushi :

    总结

    这些是不允许的。

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

    他们会发出这样的警告:

    Deprecation警告:无效的转义序列 \.

    然而,所有这些都是允许的,并且是等效的:

    # 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
    

    解释

    记住,反斜杠( \ )如果在常规字符串内部使用,则必须在Python中转义char本身( 'some string' "some string" )而不是 raw string ( r'some string' r"some string" )。因此,请记住您正在使用的字符串类型。转义句点( . )因此,在正则python字符串的正则表达式中,还必须使用双反斜杠来转义反斜杠( \\ ),为 在正则表达式中: \\. ,如上面的示例所示。

    参考文献

    1. 主要和官方参考: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals enter image description here
    2. [由@Sean Hammond回答] How to fix "<string> DeprecationWarning: invalid escape sequence" in Python?

      如果你想放一个文字 \ 在你必须使用的字符串中 \\

        5
  •  1
  •   Ali Abul Hawa    6 年前

    要转义字符串变量的非字母数字字符,包括句点,可以使用 re.escape :

    import re
    
    expression = 'whatever.v1.dfc'
    escaped_expression = re.escape(expression)
    print(escaped_expression)
    

    输出:

    whatever\.v1\.dfc

    您可以使用转义表达式来查找/匹配字符串。

        6
  •  -3
  •   Emma    6 年前

    这个表达式,

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

    对于那些特定类型的输入字符串,可能也可以正常工作。

    Demo

    测验

    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)
    

    输出

    ['test.this']
    

    如果您希望简化/修改/探索表达式,请参阅 regex101.com 。如果你愿意,你也可以在 this link ,它将如何与一些样本输入相匹配。


        7
  •  -3
  •   Gabriel Staples    5 年前

    在javascript中,您必须使用 \\. 以匹配一个点。

    实例

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

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