RE match fail in python, confuse with the result on regex101

后端 未结 2 825
攒了一身酷
攒了一身酷 2021-01-25 10:02

http://regex101.com/r/oU6eI5/1 , test here seam works, but when i put in Python, match whole str.

str = galley/files/tew/tewt/tweqt/
re.sub(\'^.+/+([^/]+/$)\', \         


        
相关标签:
2条回答
  • 2021-01-25 10:15

    It would be better if you define the pattern or regex as raw string.

    >>> import re
    >>> s = "galley/files/tew/tewt/tweqt/"
    >>> m = re.sub(r'^.+/+([^/]+/$)', r'\1', s)
                   ^                  ^
    >>> m
    'tweqt/'
    
    0 讨论(0)
  • You need to use a raw string in the replace:

    str = galley/files/tew/tewt/tweqt/
    re.sub('^.+/+([^/]+/$)', r"\1", str)
    #                        ^
    

    Otherwise, you get the escaped character \1. For instance on my console, it's a little smiley.

    If you somehow don't want to raw your string, you'll have to escape the backslash:

    re.sub('^.+/+([^/]+/$)', "\\1", str)
    

    Also worth noting that it's a good practice to raw your regex strings and use consistent quotes, so you I would advise using:

    re.sub(r'^.+/+([^/]+/$)', r'\1', str)
    

    Other notes

    It might be simpler to match (using re.search) instead of using re.sub:

    re.search(r'[^/]+/$', str).group()
    # => tweqt/
    

    And you might want to use another variable name other than str because this will override the existing function str().

    0 讨论(0)
提交回复
热议问题