Stereonaut!

Archive for the ‘regex’ tag

On Twitter alerts and regular expressions

with 3 comments

I don't know if I'm the only one that does something like this, but I certainly trust I'm not alone.

I love regular expressions (to which some people say there's nothing regular about) and I just found TweetBeep recently, which is a simple alerts system. There's nothing really new or innovative about it, given that you can get the results yourself using advanced search on Twitter and consuming the results from a feed. However, I for sure, I don't want to spend time setting any of that up, and they do that quite easily, which is the whole difference, ease of use. You can get the results on time intervals via mail. Rocks.

So, one of my alerts is about regular expressions: I'm receiving everything Twitter tweets about them, whether regex, regexp, regexes, regexps, etc. are mentioned, I get them. Now, I feel a bit nerdish/dorky about it given that I like to reply on those tweets on totally random people and try help them whenever it's possible (or just learn a bit more from other regex gurus), but what the hell.

Now, the good thing about this is that being a "good person" and fellow regex buddy with people is starting to pay off. Whether I'm getting new very interesting followers, I also found a nice opportunities niche to take advantage of, and a couple of gigs have come up. I worship building strong relationships with people (as in long-term clients), and when starting to get to know some persons via a common interesting thing such as a regular expressions is just awesome, little piece of awesomeness.

For the geeky part of this post, if my regular expression Twitter alert had to be written as a regex, this would be it:

/regex(?:(ps?|es))?/

The questions now is, should I start following /regular\s*expres{1,2}ions?/ too? Maybe I will :-)

Written by David Moreno

April 12th, 2009 at 7:17 am

Categorized in: nerd,planeta linux,regex,twitter

Tagged with , , ,

Regular expressions capturing in Java for Perl hackers

without comments

Capturing is one of the more interesting things you can do with regular expressions, it gives you great power to get sub-strings out of strings with regex power.

I recently came up with the need to do it with Java. Having Perl background, you can understand how awful any other regular expressions implementation can be after been hacked with Perl's, but it's alright, it's cool to learn.

Anyway, it's just simple, you want to capture Perl's $0, $1, $2, etc, in your Java class. Not a problem.

On my example, an HTML string needs to be scrapped. Let's say…

<a href="http://google.com/">Visit Google today!</a>

We want to get both the URL and the string between the <a> and </a> tags.

So, after all it's very simple. You have your inputStr string, and you compile your regex patternStr string as a Pattern object. Then, create a Matcher object crossing both. If we find any matches, we'll make a for loop that will iterate on matcher's grouping, printing each group at a time.

I hope some of you Perl hackers, find this information helpful when dealing with Java and need to group/capture your strings.

Written by admin

March 16th, 2009 at 3:16 pm

Categorized in: java,perl,planeta linux

Tagged with , , ,