如何获取字符从第二个到最后一个实例(不包括最后一个实例)之后的所有文本?

2024-06-16 12:00:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下部分URL:

"/tennis/qatar/atp-doha-2009/hernych-jan-monfils-gael-S8Lm3D4l/"

我希望在倒数第二个/之后获得所有内容,并排除最后一个/,因此:

"hernych-jan-monfils-gael-S8Lm3D4l"

我已经做到了:

re.search(r".*/(.*?/.*)", url)

这让我感到:

"hernych-jan-monfils-gael-S8Lm3D4l/"

但我想不出如何去掉最后的斜杠。有人能给我指出正确的方向吗


Tags: reurl内容searchjan倒数gaelatp
3条回答

您可以这样做:

s = "/tennis/qatar/atp-doha-2009/hernych-jan-monfils-gael-S8Lm3D4l/" 
s = "/".join(s.split("/")[-2:])  # Equivalent to your regex, with replace
s = s.rstrip("/")  # to remove the last slash

对于更具Python风格的方法,您还可以使用:

"/tennis/qatar/atp-doha-2009/hernych-jan-monfils-gael-S8Lm3D4l/".split('/')[-2]

str.split输出使用提供的分隔符分割的单词列表(在本例中为'/')。因此,为了打破上述说法

s = "/tennis/qatar/atp-doha-2009/hernych-jan-monfils-gael-S8Lm3D4l/".split('/')

print(s)
>> ['', 'tennis', 'qatar', 'atp-doha-2009', 'hernych-jan-monfils-gael-S8Lm3D4l', '']

print(s[-2])
>> 'hernych-jan-monfils-gael-S8Lm3D4l'

使用

^.*?/([^/]*)/?$

proof

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^/]*                    any character except: '/' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  /?                       '/' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

Python code

import re
regex = r"^.*?/([^/]*)/?$"
text = "/tennis/qatar/atp-doha-2009/hernych-jan-monfils-gael-S8Lm3D4l/"
print(re.findall(regex, text))

结果:['hernych-jan-monfils-gael-S8Lm3D4l']

相关问题 更多 >