I have a few files in the folder and I want to find out the content and match its file name. But when I use re.search to achieve my goal I can only get ‘None’. Anyone could help?
import re
xe = r'D:\ABC\cc123.xml'
re.search('cc*?.xml', xe)
I have a few files in the folder and I want to find out the content and match its file name. But when I use re.search to achieve my goal I can only get ‘None’. Anyone could help?
import re
xe = r'D:\ABC\cc123.xml'
re.search('cc*?.xml', xe)
(Though there is an accepted answer, I don't feel the answer is clear for other people, and there is still room to improve, so I added a new answer here)
The problem is simply OP is using a wrong regex: cc*?.xml
* means any occurrence of the preceding token (which means c in your case)
*? is a reluctant match any occurrence.
. means any character
Which means what you are trying to do is match a string which is:
cxmlExample of matching strings are c.xml ccccccAxml etc.
What you were trying to do, I believe is
cc.*?\.xml
which means matching
cc.*? : followed by any occurrence of any character, matching as few as possible\. : followed by a dot (note the difference of \. vs .)xml