tag archive: regex

findr: a GNU/Unix ‘find’ helper

I’ve been more aggressive lately about creating helper scripts and aliases for common tasks, and I liked this find helper enough to want to share it. I find that I often do filename searches like this:

find -iname “*some_word*”

That’s not so bad to type, but why not make it even easier for my, and maybe your common usage? We might prefer to do something like this:

findr some_word

And assume that we’re looking for a case-insensitive, substring match. And since we love regular expressions, we can work with find’s regex options:

–regextype posix-egrep -iregex

Which is just begging for a helper to do the work for us in standard situations.

Before we look at the script and some examples, here are a few more considerations for our findr command:

With the -iregex option, the match is against the whole path, so we’ll wrap the supplied regex search term with dot-stars (.*) to loosen things up. We probably want to limit our match to the last item …

Python script for backing up Twitter statuses

I was looking for a way to backup my tweets (I find that I’m reluctantly giving over to calling Twitter posts by that name, but I don’t feel good about it) and found this “recipe” at ActiveState’s web site:

Recipe 576594: Backup/download your tweets or anyone’s tweets.

It’s a nice simple Python script that saves messages to a text file. Works great, although I wanted some more. It doesn’t save timestamps and also leaves HTML link tags in there. The recipe also introduced me to a nifty library, “Beautiful Soup“, which is a “Python HTML/XML parser designed for quick turnaround projects like screen-scraping.”

Since I’ve been intending to get back into learning and using Python, this was a good opportunity to play around a bit. Starting from Zach’s code and with the nice Beautiful Soup documentation, it didn’t take long to figure out how to extract more from the Twitter pages. Also there was some fun with regular …

Regex Walk-Through: Match filename base and extension

This post is a rather lengthy analysis of a short regular expression:

(.+?)(\.[^.]*$|$)

I’m continuing to enjoy and learn a lot from Jeffrey Friedl’s Mastering Regular Expressions. One of the things that works well for me is the way he walks through examples and iteratively builds a more robust pattern for a particular task. It helps to develop logical ways of thinking about these things.

While you may not need to accomplish the particular task described in this article, I hope you might benefit by following along with the explanation. (Although it’s probably not as clear and concise as the ones in MRE!)

So! I recently wanted to modify a filename by inserting some text before the extension and was pleased with the regular expression I built for the task. Here it is again in all its glory:

(.+?)(\.[^.]*$|$)

The two capturing groups here will collect (with an exception explained below):

Everything up to but not including the last …

Python: Regex Test Function

 

More fun with Python and regular expressions. Following up on a previous post, I wanted to share a little test regex function I wrote in Python to help me as I work through the regular expression book.

I’m mostly working at the interactive prompt and had been running commands from Python re (the regex module) as I experimented with different regular expressions. This was good as I spent time in help(re) and built up some muscle memory for Python regex functions, but it was becoming repetitious to keep typing the commands for analyzing the results of a match. Once I started learning about writing functions in Python, I realized it was time to enhance my regex learning experience with a simple Python function.

I know there are sophisticated regex tools out there and probably simple functions that do more than this, but it was fun to …

Goals: Learning Python and Regular Expressions

I don’t want to go all 7 Habits of Highly Effective People on you, but I’ll say this: I think it’s good to have goals and to work towards your goals.

I like this quote, which I’ve seen attributed to Zig Ziglar:

“Most people fail to reach their goals not because their plans are too simple or too complicated. Most people don’t reach their goals because they’re not committed and willing to follow their plans.”

I think that’s very true. There are so many things I haven’t done because I didn’t follow through on a plan.

What this means for you, loyal reader, is that one of the reasons I’m not accomplishing the goal of writing more for the web site (including a write-up of my trip to Boston for the FSF meeting) is that I’m working towards other goals of learning Python and more about regular expressions.

These two books are great: