-1

I am looking for a way to capture the following with either a regular expression or a built-in function in Python.

From /url-path/YYYYMMDD-N/url-path-cont I only need YYYYMMDD-N. Sometimes the -N is present and sometimes it is not. I have tried various methods but so far all my attempts either stop at YYYMMDD or capture part of /url-path-cont.

I would like to capture only the YYYYMMDD-N with the -N as optional whenever present.

Freddy
  • 511
  • 2
  • 9
  • 19
  • Does this answer your question? [Python split string into multiple string](https://stackoverflow.com/questions/9703512/python-split-string-into-multiple-string) –  Jul 17 '21 at 21:46

2 Answers2

2

There are probably better ways of doing this, but as long as there's always the same amount of / then you could use the split method:

url_path = "/url-path/YYYYMMDD-N/url-path-cont"
date_only = url_path.split("/")[2]
print(date_only)
Luke
  • 226
  • 1
  • 10
  • This doesn't answer the question, the OP asked specifically for a regular expression (regex) –  Jul 17 '21 at 21:35
  • 1
    @Luke Thank you. Your solution worked perfectly. That is the result I was looking for. – Freddy Jul 17 '21 at 21:37
  • 2
    Reword your question then, and remove the term regex, if this is the answer your looking for. –  Jul 17 '21 at 21:38
  • @Jared I will edit my question so that it's not specific to regular expressions and to not cause confusion. – Freddy Jul 17 '21 at 21:38
1

Here is a regular expression that will extract the date from a string.

>>> import re
>>> url = "url-path/YYYYMMDD-N/url-path-cont"
>>> re.compile(r"/(\w+-?\w?)/").search(url).group(1)
'YYYYMMDD-N'
>>>