Sunday, July 01, 2007

Taste regular expressions; fiend!

=~/JOB ID:[^{]+{([^}]+)}\s\(/;

HA!

7 comments:

blogagog said...

What do square brackets do in a substitution again? I'm an engineer and only get to do this stuff on rare occasions. The uphat means beginning of line, right? I'm lost.

=~ /\s/ work it harder make it better\ndo it faster makes us stronger\n/g;

Ya JERK! That song has been stuck in my head all weekend!

Fletch said...

Square brackets are character lists that match for one character or a set [A|a] when preceeded by a carrot (^) it negates the selection.

The match is set against many lines that resemble
onoz: {34534-345} (oorgle)
thing: {34534-345-3453-4} (wiffle)
lolwut: {34534-4534-5} (eggs)

Splitting the lines would make the match easy, but who wants to run another loop? So you have to write the match so it can only match one of the lines which means the regex has to be exact.
Match "JOB ID:" followed by one or more non "{" characters, followed by a "{" character followed by one or more non "}" character, followed by a space, followed by a "(".

Most matches you can get away with .+ until you find another match, but not when you have a lot of similar lines.

blogagog said...

Uphat, existingthing. It's called an uphat.

blogagog said...

Ok I still don't get it. I kinda sorta remember you could say [a-zA-Z] to catch any letter. Would adding a carrot make it any character that's NOT a letter?

If so, why... ok, I'm starting to get it. But why use [^{]+ instead of .* ?

Full time coders are mystical to me. My uphat is off to you.

blogagog said...

Ok, now I get it :), and it only took 24 hrs. Thanks for making me study page 25 of the pink book again.

Fletch said...

Pink book? Which is this? I'm only familiar with the pink shirt book.

blogagog said...

It's an O'Reilly nutshell book on perl. The first one was pink, the second blue (there's prolly a dozen more since then). If perl has new functions since 1995, I'm blissfully unaware of them ('cept LWP which is all I really use it for these days).