'python regex for filename

I am trying to do a regex on a dataframe.

For example a value will be ia wt template - tdct-c15-c5.doc The best logic I can think of is to take everything after the - till the last digit in the string.

trying to trim it to tdct-c15-c5

any help would be appreciated.



Solution 1:[1]

I don't know if a regex is the better option here. An apply is pretty readable:

mystr = "ia wt template - tdct-c15-c5.doc"
import pandas as pd

df = pd.DataFrame([[mystr] for i in range(4)], columns=['mystr'])

df.mystr.apply(lambda x: x.split(' ')[-1].rstrip('.doc'))
0    tdct-c15-c5
1    tdct-c15-c5
2    tdct-c15-c5
3    tdct-c15-c5
Name: mystr, dtype: object

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 C.Nivs