regex - Python: Reggular Expressions filtering for Self Learning program -
i making program has small way of self learning, want "information" output like:
>>>#ff0000 hexcode color red
i want filter reggular expressions
user filled sentence is hexcode color
, , retrieve name of color , hexcode. have put small code below how want works:
#main.py strinput = raw_input("please give fact:") if "{0} hexcode color {1}" in strinput: # {0} name of color # {1} hexcode of color print "you give me color" if "{0} vehicle" in strinput: # {0} vehicle print "you give me vehicle"
is possible reggular expressions
, , best way reggular expressions
?
you can read regular expressions in python in standard library documentation. here, i'm using named groups store matched value dictionary structure key choose.
>>> import re >>> s = '#ff0000 hexcode color red' >>> m = re.match(r'(?p<hexcode>.+) hexcode color (?p<color>.+)', s) >>> m.groupdict() {'color': 'red', 'hexcode': '#ff0000'}
note if there's no match using regular expression, m
object here none
.
Comments
Post a Comment