March 10th, 2008

Excuse me while I GREP in your face

Filed under: daily,geek,webdev — saul @ 7:11 AM

I don’t hide the fact that I’m a geek, for a designer, I think I hold my own with most webdev programmers - In most regards I know my shit, but until recently I haven’t spent anytime learning regex (regular-expressions) and let me tell you, if you don’t know regex - simply put, you’re working too hard.

Regex is a way to filter strings that match a pattern out of text from a file or from standard input. Regular expressions (or RegExs) can be used with a big choice of programs, most notably grep (the global regular expression parser) and sed (the stream editor).

I know, even the definition is somewhat intimidating, but the power of regex is worth the pain I promise - let’s look at an example, take a look at the following list of fish (assuming that the number was a quantity).

  1. One Fish: 1
  2. Two Fish: 15
  3. Red Fish: 9
  4. Blue Fish: 0


And what we wanted to do was display the list but eliminate any fish that had a quantity of zero, throw a regex like ‘^.*0.*$’ at your text and you get back, now there is a flaw (it’s intentional) in my expression here, but I’ll let you figure it out.

  1. One Fish: 1
  2. Two Fish: 15
  3. Red Fish: 9

No looping, no conditional statements - just raw regex power, rather than fumble through a less-than-adequate explanation here’s 3 spectacular links to get you started.

If you come up with anything cool let me know

1 Comment

  1. If you’re going to be playing with these, I highly suggest getting RegExhibit: http://homepage.mac.com/roger_jolly/software/index.html

    Comment by Dan — 03.11.08 @ 2:27 PM

Sorry, the comment form is closed at this time.