cs212 »

Help with Regular Expressions

Note — this is just a start. Please add to this.

Regular expressions confuse people and you need to understand them a little to do unit 3. Here are some resources that might help.

'Regular expressions' is a bit of a term of art. Think of it as a tiny language to describe increasingly arbitrary strings of text. A bit like loads of wildcards all strung together. So for example, /gr[ae]y/ matches 'grey' or 'gray', and /colou?r/ matches 'colour' or 'color'.

They're bolted onto Python in a module called re.

You might regard Unit3 as addressing the problem, "What if we did not have regular expressions? What if we were to try to develop our own special grammar for describing string patterns?" (At some point this perspective breaks down, however, since we actually use regular expressions to define our grammars. At that point we switch back to understanding Unit3 as an exploration of how languages and functions can be used as tools.)

Norvig defines functions such as lit, alt, star and plus which can be combined through function composition in order to describe string patterns. These function have direct analogues in the Python re module.

For example,

Novig's APIre module API
searchre.search
matchre.match
alt(x,y)"x|y"
star(x)"x*"
plus(x)"x+"

You can learn more about the re module API through the resources listed below:

Regular Expression Resources

http://docs.python.org/howto/regex.html - This is the Python 'introductory guide' to regular expressions, designed for people who find the regular documentation too hard. It's not that straightforward even so.

http://en.wikipedia.org/wiki/Regular_expression - The Wikipedia page on regular expressions.

http://www.regular-expressions.info/quickstart.html - A regular expressions explanation in one (very long) page — this site also has a tutorial.

http://gskinner.com/RegExr/ - This is an online generator and tester for regular expressions, so you can stick some test in and play about with regular expressions.

http://blog.themeforest.net/screencasts/regular-expressions-for-dummies/ - This is a series of videos that introduces regular expressions in a very gentle way. Good for people who are still confused after trying other tutorials.

http://www.udacity.com/view#Course/cs262/CourseRev/apr2012/Unit/3001/Nugget/15002 - Udacity CS 262 (Programming Languages) Unit on Regular Expressions.

https://developers.google.com/edu/python/regular-expressions Google's Python class about regular expressions. Also has lecture on YouTube