Difference between revisions of "RegEx Pattern Matching"
(Created page with "<pre class=setup> #ENCODING import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-16') #MONGO from pymongo import MongoClient client = MongoClien...") |
|||
Line 54: | Line 54: | ||
<div class=ans> | <div class=ans> | ||
pp.pprint(list(db.world.find({"name":{'$regex':"x"}},{"name":1,"_id":0}))) | pp.pprint(list(db.world.find({"name":{'$regex':"x"}},{"name":1,"_id":0}))) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | Iceland and Switzerland end with <b>land</b> but where are the others? | ||
+ | <p class=strong>Find the countries that end with land</p> | ||
+ | <pre class=def> | ||
+ | pp.pprint(list( | ||
+ | db.world.find({"name":{'$regex':"stan$"}},{"name":1,"_id":0}) | ||
+ | )) | ||
+ | </pre> | ||
+ | <div class=ans> | ||
+ | pp.pprint(list(db.world.find({"name":{'$regex':"land$"}},{"name":1,"_id":0}))) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | Columbia starts with a <b>C</b> and ends with <b>ia</b> - there are two other countries like this.<br/> | ||
+ | You can use <code>.*</code> to match any character except newlines. | ||
+ | <p class=strong>Find the countries that start with C and end with ia</p> | ||
+ | <pre class=def> | ||
+ | pp.pprint(list( | ||
+ | db.world.find({"name":{'$regex':"^A.*n$"}},{"name":1,"_id":0}) | ||
+ | )) | ||
+ | </pre> | ||
+ | <div class=ans> | ||
+ | pp.pprint(list(db.world.find({"name":{'$regex':"^C.*ia$"}},{"name":1,"_id":0}))) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | Greece has a double <b>e</b>, who has a double <b>o</b><br/> | ||
+ | <p class=strong>Find the countty that has oo in its name</p> | ||
+ | <pre class=def> | ||
+ | pp.pprint(list( | ||
+ | db.world.find({"name":{'$regex':"ee"}},{"name":1,"_id":0}) | ||
+ | )) | ||
+ | </pre> | ||
+ | <div class=ans> | ||
+ | pp.pprint(list(db.world.find({"name":{'$regex':"oo"}},{"name":1,"_id":0}))) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | Bahamas has three <b>a</b>, who else?<br/> | ||
+ | <p class=strong>Find the countty that has oo in its name</p> | ||
+ | <pre class=def> | ||
+ | pp.pprint(list( | ||
+ | db.world.find({"name":{'$regex':"^T"}},{"name":1,"_id":0}) | ||
+ | )) | ||
+ | </pre> | ||
+ | <div class=ans> | ||
+ | pp.pprint(list(db.world.find({"name":{'$regex':"a.*a.*a.*"}},{"name":1,"_id":0}))) | ||
</div> | </div> | ||
</div> | </div> |
Revision as of 17:04, 15 July 2015
#ENCODING import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-16') #MONGO from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] #PRETTY import pprint pp = pprint.PrettyPrinter(indent=4)
Pattern Matching String
This tutorial uses RegEx to check names. We will be using find() on the collection world.
You can use '$regex':"^B"
to get all the countries that start with B.
Find the countries that start with Y
pp.pprint(list( db.world.find({"name":{'$regex':"^F"}},{"name":1,"_id":0}) ))
pp.pprint(list(db.world.find({"name":{'$regex':"^Y"}},{"name":1,"_id":0})))
You can use '$regex':"a$"
to get all the countries that end with a.
Find the countries that end with Y
pp.pprint(list( db.world.find({"name":{'$regex':"l$"}},{"name":1,"_id":0}) ))
pp.pprint(list(db.world.find({"name":{'$regex':"y$"}},{"name":1,"_id":0})))
Luxembourg has an x, so does one other country, list them both
Find the countries that contain the letter x
pp.pprint(list( db.world.find({"name":{'$regex':"ana"}},{"name":1,"_id":0}) ))
pp.pprint(list(db.world.find({"name":{'$regex':"x"}},{"name":1,"_id":0})))
Iceland and Switzerland end with land but where are the others?
Find the countries that end with land
pp.pprint(list( db.world.find({"name":{'$regex':"stan$"}},{"name":1,"_id":0}) ))
pp.pprint(list(db.world.find({"name":{'$regex':"land$"}},{"name":1,"_id":0})))
Columbia starts with a C and ends with ia - there are two other countries like this.
You can use .*
to match any character except newlines.
Find the countries that start with C and end with ia
pp.pprint(list( db.world.find({"name":{'$regex':"^A.*n$"}},{"name":1,"_id":0}) ))
pp.pprint(list(db.world.find({"name":{'$regex':"^C.*ia$"}},{"name":1,"_id":0})))
Greece has a double e, who has a double o
Find the countty that has oo in its name
pp.pprint(list( db.world.find({"name":{'$regex':"ee"}},{"name":1,"_id":0}) ))
pp.pprint(list(db.world.find({"name":{'$regex':"oo"}},{"name":1,"_id":0})))
Bahamas has three a, who else?
Find the countty that has oo in its name
pp.pprint(list( db.world.find({"name":{'$regex':"^T"}},{"name":1,"_id":0}) ))
pp.pprint(list(db.world.find({"name":{'$regex':"a.*a.*a.*"}},{"name":1,"_id":0})))