Monday, June 16, 2014

Have no fear, I know regular expressions!

I know regular expressions, and at last get the joke to the XKCD comic:


( © Randall Monroe)

It does feel a little bit like I know kung fu. Regular expressions, which (essentially) search for a given pattern in a string, i.e. a set of characters (words, numbers, etc.), and then let you do something with the results. These powerful, albeit cryptic, expressions allow you to radically simplify many operations on strings.

Take, for instance, a problem presented on Coderbyte: given a string of words or letters, shift every letter over by 1 (so 'c' becomes 'd', 'z' becomes 'a'), and then capitalize any vowels excluding 'y'.

Here is a solution found sans regular expressions. Just gloss over the fine print for now and look at the size of it.


Here is a solution found using regular expressions (namely, mine).


Slight difference, right? As an added benefit, regular expressions can often let you write my like how you would talk about solving the problem:

1. There's this function called 'LetterChanges,' you pass it a string.

2. Give me the result of taking all the alphabetic characters in the string and doing the following with each one:

3. If it's Z, change it to A

4. If it's z, change it to z

5. Otherwise shift the character over by one

7. Replace all the vowels with …

8. … an uppercase version

Neat, huh?

No comments: