0

I am trying to capture some text in a string shown below

testdir1\testdir1\textfile.vhd

I want to capture the file extension plus the dot and i am using the regex

[.vhd]{4}$

This seems to work on regex101

https://regex101.com/r/TSoIN3/2

but when i actually implement this in python it doesn't

import re
result = re.match(r'[.vhd]{4}$',"testdir1\\testdir1\\textfile.vhd")
print(result)

None

I am assuming that the regex is essentially working but i am calling the python match function wrong???

any help would be appreciated.

user254340
  • 447
  • 2
  • 7
  • 21
  • 1
    Why not use `endswith`? Also, see http://ideone.com/LOso3R – Wiktor Stribiżew Apr 24 '17 at 19:51
  • 2
    RTFM: re.match: [If zero or more characters at the beginning of string match the regular expression pattern](https://docs.python.org/3/library/re.html#re.match) – Sebastian Proske Apr 24 '17 at 19:51
  • 2
    Even within the realm of regexes, `[.vhd]{4}` is wrong and overcomplicated. That matches `....`, `dhv.`, and all sorts of other strings you shouldn't match. The character class and the `{4}` aren't appropriate; it should just be `\.vhd`. – user2357112 Apr 24 '17 at 19:54
  • ah. thanks so it looks like i should use search instead of match... should have read the docs closer – user254340 Apr 24 '17 at 19:56

0 Answers0