4

I am new in the regular expression and trying to extract the file name from a string which is basically a file path.


string = "input_new/survey/argentina-attributes.csv"
string_required = argentina-attributes

i know i can do this by below code.

string.split('/')[2].split('.')[0]

but I am looking to do this by using regular expression so if in future the formate of the path changes(input_new/survey/path/path/argentina-attributes.csv) should not affect the output.

i know kind of similar question asked before but I am looking for a pattern which will work for my use case.

om tripathi
  • 300
  • 1
  • 5
  • 20
  • Look [here](https://stackoverflow.com/a/52966261/3832970), `r'^.*[\\/](.+?)\.[^.]+$'` looks to be what you want. – Wiktor Stribiżew Oct 01 '19 at 09:28
  • I suggest you use the [pathlib](https://docs.python.org/3/library/pathlib.html) library (in the std library), it is meant to manipulate paths and is (in my opinion) very convenient. A simple `Path(string).name` will do – Plopp Oct 01 '19 at 09:30
  • `>>> re.search(r"^.*[\\/](.+?)\.[^.]+$", "input_new/survey/argentina-attributes.csv").group(0) 'input_new/survey/argentina-attributes.csv' not working – om tripathi Oct 01 '19 at 09:34

2 Answers2

5

Try this,

>>> import re
>>> string = "input_new/survey/argentina-attributes.csv"

Output:

>>> re.findall(r'[^\/]+(?=\.)',string) # or re.findall(r'([^\/]+)\.',string)
['argentina-attributes']

Referred from here

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
2

Try this:

string = "input_new/survey/argentina-attributes.csv"
new_string = string.split('/')[-1].split('.')[0]
print(new_string)
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23