Me + Regex = Happy

I was going to write a post about how awesome Regular Expressions are with lots of examples. I spent a lot of last week nailing down the years of data bits that were floating around in my brain. I’d known a bit about how to use them but hadn’t really understood the structure. Well, the structure is awesome and I’m mid-way through comprehending it.

But instead of writing the post, I went and spent over an hour writing 3 in-depth tutorials for another site on how to use Regex for some specific stuff and now all my brain can think is “Regular Expressions — yay!”

If you have any reason at all to go near Regular Expressions, I suggest looking at this helpful quickstart, if nothing else. It outlines some basic syntax and shortcodes with links to more detail on each section. And combing through that site is a good place to start.

What’s Regex good for? It’s handy for doing things in bulk when you’re not doing exactly the same thing each time. For example, a quick find & replace will work if you’re trying to remove “2010/05/31” from all your posts’ links to the url “http://arkhamarchivist.com/2010/05/31/post-title/”.

But if you’ve got 900 posts all of which link back to each other, which were published over a long string of dates, and which used to use dates in their URLs but now just use postnames, then you can either put in every date that happened from start to finish or write an expression for it and get it done in 30 seconds.

You can use Regex to write .htaccess redirects (or redirects in the Redirection plugin) if you change something about your sites link structure.

They’re also helpful if you’re trying to find every time you used multiple forms of the same word or match just about anything. If you’re looking in a small document, you can eyeball it or use multiple searches, but if you’re trying to find something in a database, it’s a huge help.

If I’m editing blog posts, I use the Search Regex plugin and if I’m editing databases or other files, I use the Regular Expressions function in search in Notepad++.

Regular Expressions — yay!!

(More ideas/tutorials/etc on using Regex to follow at some point. Right now, I’m just sharing the happy.)

9 Comments

  1. Jovana says:

    I looked through the site and it seems very complicated, haha.
    .-= JovanaΒ΄s last blog ..The Ultimate June Round Up of Competitions for Geeks – Bookmark this! =-.

  2. Carl says:

    Regular expressions? You’re playing with fire there… A good regex will be one typo away from looking like modem line noise, and another typo away from looking like some unpronounceable incantation that’ll summon something ancient from the Deep. Judging by your site, the latter may have already happened!

    But yeah, as scary as they are, it feels pretty good to do in 15 characters what mortals would need hours to do using basic search-and-replace.

    Also, Notepad++? If you’re doing regular expressions, you should be using a real editor like Vi!

  3. Ruth says:

    @Jovana it is, but if you’re just using it for searching it’s not too dangerous to play around. If you have backups.

    @Carl I will neither confirm or deny incantations. πŸ˜‰

    There was definitely a lot of fire involved. I spent a hell of a lot of time playing around with the things I was trying to execute on test sites & test files before I ever tried it live. And I used backups too. First in-the-field work was a success, second is tonight. *crosses fingers*

  4. Alan says:

    “Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.” – Jamie Zawinski

    Just playing devil’s advocate πŸ˜€

  5. Ruth says:

    Ha. :) It’s true, some thing they’re just not the best choice for and sometime I was overthinking them (I had to use them for three specific things). That’s why I always test!! Or at least have backups. πŸ˜‰

  6. Alan says:

    Amen, sister. Nature (particularly in IT) has this funny way of screwing you over precisely when you’ve got no backup plan; “funny” as in “open that window so I can jump.”

    As for RegEx’s, you’re spot on. I’ve seen them utterly abused…the worst case being a full printed page for validating a credit card number.

    Seriously. One full page.

    Who the hell is going to debug that? It’s not a matter of being a good developer at that point. When you have a RegEx that fills a page, it’s just freakin’ dense and you don’t dare touch it for fear of bringing down the Mir space station.

  7. Ruth says:

    A page of Regex would overload my logic circuits. My mom might be coldly logical enough to work through it (she loves this kind of stuff and is the kind of person to do linear algebra for kicks & giggles).

  8. eemusings says:

    My knowledge is pretty much limited to bolding, italics, linking, image insertion and simple tables. Oh, and I just learned the subscript tag. I think I could learn a lot from ya :)

  9. Ruth says:

    Then I wouldn’t advise regular expressions just yet. πŸ˜‰ That’s like moving from bike to a car. Fortunately, learning more html/css isn’t too hard with all the great resources out there (I love w3schools.com).

Leave a Reply

Your email address will not be published. Required fields are marked *