parik parik - 1 year ago 84
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:


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

Answer Source

"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 =, string)
if match:
    print 'we\'ve got a match!'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download