I have to validate next string format:
text-text-id-text
Separator is character '-'. Third column must always be id. I wrote next regex (in python) which validates string:
import re
s = 'col1-col2-col3-id' # any additional text at the end
# is allowed e.g. -col4-col5
print re.match('^(.*-){3}id(-.*)?$', s) # ok
print re.match('^(.*-){1}id(-.*)?$', s) # still ok, is should not be
I tried adding non-greedy mode, but result is still the same:
^(.*?-){1}id(-.*)?$
What am I missing in my regex? I could just validate string like this:
>>> import re
>>> print re.split('-', 'col1-col2-col3-id')
['col1', 'col2', 'col3', 'id']
And then check if the third element matches id, but I am interested in why does the first regex works as mentioned above.