Regex madness!

Miscellaneous Forums/General Discussion/Regex madness!

I keep bumping into stuff that suggests I "simply write a regular expression" to do all sorts of insanely complicated tasks. These are not simple, or regular, expressions!
...Although they end up horrifyingly short given what they do (and the fact that they have debuggers). There are clearly a lot of ugly symbols in these things.

I do not understand regex, and that seems to mark me as the equivalent of an illiterate with some of the systems out there.

The best solution is undoubtedly to learn this madness. Do you know any good books (preferably below 1000 pages), or tutorials, or web sites that teach how to write regular expressions, how they work, etc.?

Alternatively (preferably, because regex scares me), is there a magical program that will do all the work for me?

Oh, and why are regular expressions a seemingly universal thing? Are these done by a single standard library, or are they just a widely accepted standard that nobody messes with?

I've never mucked with them, but I could probably grok them if given 30 minutes head-start. They at least follow easily definable logical rules, which is what us programmers are supposed to understand, right?

I don't understand regex, too. It's clever and logical to hell, but the first word that comes in mind when seeing regex is "brainfuck".

Google for "regex builder", you'll get lots of links to programs that might help you.

I love regex =)

Sure it looks scary the first few times, but once you get to know it its actually pretty simple.

This might help you on your adventure. http://regex.powertoy.org/ , good luck ;) hehe

Regex rocks ;-)

It's an awesome tool for search/replace...

There's an extensive regex guide with my pcre-based BlitzMax regular expressions module.

I guess the requirement to learn regex is inversely proportional to the inverse square of the complexity of your string handling requirement ;)

are they just a widely accepted standard that nobody messes with?

There are two main camps, Perl compatible regular expression, and POSIX regular expressions.
They share a lot of similarities, but do have subtle differences. Usually, if you can do one, the other is pretty easy.

For some regular expression fun I suggest trying "vi" (or vim), "sed" and "awk" on *nix.

:-)

Regular Expressions are a great tool. I don't really know of any book that teaches it, but http://www.regular-expressions.info is as good a place to start as any.

Oops, I never said thanks!
Thanks everyone, this is very helpful :)

I was also pointed to a book via Django's documentation: “Mastering Regular Expressions” by Jeffrey Friedl

I do not understand regex, and that seems to mark me as the equivalent of an illiterate with some of the systems out there.


Regular Expressions seem a bit odd at first, but they are *extremely* powerful.

They make an insane difference when you're trying to parse text such as log files or email headers, and do conditional filtering, because they allow you to parse for very strictly defined patterns rather than just a blanket wildcard.

Back when I worked for an ISP, we had a great number of regex based filters on the mailserver that would look for certain patterns in the headers and recognize a ton of spam that way. A large percentage of spam we were seeing turned out to have a very predictable pattern in one of the headers.

they always listed a fakr origination address at a randomly generated hostname that was in the pattern <three letters><1 number><1 letter>.<randomdomain>.com (random IP address -- missing the trailing ) sign.

But best of all, the fake headers it included has a typo in them -- a missing trailing ), violating the mail standards.

Now, a regex:

Received.*[A-Z{3}][0-9][A-Z]\..*\.com \([0-9{1,3}]\.[0-9{1,3}]\.[0-9{1,3}]\.[0-9{1,3}]$



(IIRC, not tested)

And it will catch any email matching both of those conditions. (The $ at the end signifies the end-of-line, so no trailing ) character after the IP)

It will catch the following examples:


Received: from AHC3A.hotmail.com (10.12.3.182
Received: from BHC3B.aol.com (192.168.3.142
Received: from BHC3B.somerandomdomainyouneverheardof.com (102.168.3.142



but a genuine message like one of these:

Received: From POP2A.hotmail.com (10.10.123.1)
Received: From AHC3A.hotmail.com (10.12.3.182)
Received: From BHC3B.somerandomdomainyouneverheardof.com (102.168.3.142)
Received: from smtp.aol.com (10.102.12.1)
Received: from microsoft.com (11.11.11.11)



will not trigger the regex filter.

So... One regex, and an almost 25% drop in spam at the time.

suffice it to say, I was happy to find that Brucey made a RegEx module for BlitzMax, because I'm sure it will come in handy.

for those having no idea how to do REgEx, I'd recommend taking a peek at this:

http://www.regular-expressions.info/quickstart.html

It's a quickstart guide, explaining the most common regex commands and some samples of how to use them.