python uses regular expressions to get prefix methods for filenames
- 2020-12-21 18:06:59
- OfStack
When we work with files, we have a scenario where we need to manipulate a file and then generate the same file as the original file name (only the file format changes). At this point, you can use the regular expression to match the string you want.
implementation
import re
test='kasduabshdvkjASVKDV.txt'
m=re.findall(r'(.+?)\.',test)
print(m)
We use the python regular expression library, the re library, for fast regular matching. If you are not familiar with this, you can go to see relevant knowledge. Here I will briefly say r'(.+?) 'Here's the thing. Matches any character, + matches the regular expression that appears before 1 or more times, ? Matches a regular expression that appears before 0 or 1 times,\. Means it ends with. At the end. With such a matching rule, we can match the prefix of the file name we need.
(pyinstallertest) hades@ubuntu:~/project/opencv_test$ python re_name.py
['kasduabshdvkjASVKDV']
As you can see, the filename prefix was extracted successfully.