0

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)
1
  • also, for future reference, you can use something like pythex to test your regular expressions, and python regex documentation for syntax Commented Nov 12, 2018 at 2:33

2 Answers 2

2

(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:

  • a c
  • followed by any occurrence of c
  • followed by any character
  • followed by xml

Example 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 .)
  • followed by xml
Sign up to request clarification or add additional context in comments.

Comments

1

How about something like this with a small tweak?

import re
xe = 'D:\ABC\cc123.xml'
print (re.search('cc.*?.xml', xe).group())

output:

cc123.xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.