parik parik - 5 months ago 8
Python Question

How to deal with Regular expression in python?

I want to detect this format in my text file "1-321231", i mean any digit, plus '-',plus any digit,
i write this regular expression:

\d[-]\d*


but i don't know how use it in python.

Answer

"Detect" is what we call "Match". Here is a working example of how to use it:

import re
string = '1-321231'
pattern = '\d[-]\d*'
match = re.search(pattern, string)
if match:
    print 'we\'ve got a match!'
Comments