Python: Regular expression

Posted: March 14, 2016 in Python
Tags:

Hi, these are few example on my daily tasks using the regular expression in Python. The lists will be updated from time to time.

import re
text= "UPDATE `my_table` SET `col1` = '' WHERE `col_id` = '123456'"
searchObj = re.search( r"`col_id` = '\d+'",text, re.M|re.I)
if searchObj:
    print "Result : ", searchObj.group()
else:
    print "Nothing found!!"

The above code will print Result : col_id = ‘123456’

 

#Extract number from string

string = "test 123 test test"
mynumber = int(re.search(r'\d+', string).group())
print mynumber

You can download my python project at https://github.com/flakesns/python-example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s