【问题标题】:How to use lookahead and $ with Regex如何在正则表达式中使用前瞻和 $
【发布时间】:2021-11-29 05:05:33
【问题描述】:

我正在尝试获取资源的名称,我将与您分享regexr url

我的实际正则表达式:([^/]+)(?=\..*)

我的例子:https://res-3.cloudinary.com/ngxcoder/image/upload/f_auto,q_auto/v1/blog-images/5oonz9.jpg

我只想得到5oonz9

我尝试包含 $,但我不知道为什么它不起作用

【问题讨论】:

    标签: regex zapier


    【解决方案1】:

    你可以使用:

    ^.+\/(.+)\..+$
    
    • ^.+ - 从一开始就匹配尽可能多的字符
    • \/ - 匹配文字 /
    • (.+) - 匹配一个或多个字符并将它们捕获在一个组中
    • \. - 匹配文字 .
    • .+$ - 匹配字符串末尾的一个或多个字符(扩展名)

    现场演示here

    【讨论】:

      【解决方案2】:

      您不需要捕获组,只需匹配:

      (?<=\/)[^\/.]+(?=\.[^\/.]+$)
      

      Demo

      我们可以在 free-spacing 模式下编写表达式以使其自记录:

      (?<=      # begin a negative lookbehind
        \/      # match '/'
      )         # end negative lookbehind
      [^\]+     # match one or more characters other than '/'
      (?=       # begin a positive lookahead
        \.      # match '.'
        [^\/]+  # match one or more characters other than '/'
        $       # match end of string
      )         # end the positive lookahead
      

      但是,您不应为此使用正则表达式,因为 Python 提供了os.path

      import os
      
      str = 'https://res-3.cloudinary.com/ngxcoder/image/'\
            'upload/f_auto,q_auto/v1/blog-images/5oonz9.jpg'
      base = os.path.basename(str)
      print(os.path.splitext(base)[0])
        #=> "5oonz9"
      

      这里base #=&gt; "5oonz9.jpg"

      See it in action

      Doc

      【讨论】:

        【解决方案3】:

        有很多方法:

        下面几个使用python:

        #使用正则表达式:

        >>> file_name='https://res-3.cloudinary.com/ngxcoder/image/upload/f_auto,q_auto/v1/blog-images/5oonz9.jpg'
        >>> regexpr = r".*/([^\/]+).jpg$"
        >>> re.match(regexpr, file_name).group(1)
        '5oonz9'
        >>>
        

        #获取任意文件名:

        >>> regexpr = r".*/([^\/]+)$"
        >>> re.match(regexpr, file_name).group(1)
        '5oonz9.jpg'
        

        #如果有兴趣,这里有一个使用 split & take last

        >>> (file_name.split("/")[-1]).split(".")[0]
        '5oonz9'
        >>>
        

        【讨论】:

          【解决方案4】:

          由于其他答案,我找到了一个更直接的解决方案:

          ([^\/]+)(?=\.[^\/.]+$)
          

          解释:

          ([^\/]+) 不匹配 1 个或多个 '/'

          (?=\.) 向前看'.'

          [^\/.]+ 不匹配 1 个或多个 '/' 和 '.' (这是关键!!)

          $ 字符串结尾

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-09-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多