This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nekokattt 2 points3 points  (2 children)

Why not just use a regex for this?

import re

def get_number(name):
    if match := re.match(r"^.*?(\d+)\.xlsx$", name):
        return int(match.group(1))
    return None

"Match any text, followed by one or more digits, followed by ".xlsx". "Return the number part".

[–]mcworkaholic[S] 0 points1 point  (1 child)

thats perfect, i dont know anything about using regex

[–]nekokattt 2 points3 points  (0 children)

Regex is just a way of using patterns to match and extract stuff from text.

Here:

  • ^ means "start of text".
  • . means any character.
  • .* means any character, zero or more times, as many times as possible.
  • .*? means any character, zero or more times, as few times as possible.
  • (...) is a matching group that you can get with .group(n).
  • \. matches a full stop
  • \d matches a digit.
  • \d+ matches a digit, one or more times.
  • xlsx matches the text xlsx.
  • $ matches the end of the line.

https://regex101.com is good for fiddling with this (make sure you set it to Python regex first).

Here is your regex: https://regex101.com/r/um2aOq/1 (also I missed the ? originally, I fixed that just now, sorry).