Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: This looks like someone sneezed and hit the keyboard

by dws (Chancellor)
on Feb 03, 2004 at 07:23 UTC ( [id://326140]=note: print w/replies, xml ) Need Help??


in reply to This looks like someone sneezed and hit the keyboard

One way to make sense of your former employee's code is to use the /x modifier on the regex, which lets you throw in whitespace for formatting without screwing up semantics (except that you'll need to encode whitespace as \s -- thanks, grinder). With this, the regex will look something like
m/ .* ( [\$#\%>~] | \@\w~\$ | \\ \[ \\ e \[ 0m \\ \] \s \[ 0m ) \s? /x
which basically says
  • skip past as many characters as possible, then
  • match one of three things
    • a single character that is one of $ # % > ~
    • a four character string beginning with @, following by a single "word" character, followed by ~ and $
    • the string "\[\e[0m\] [0m"
  • allow for an optional trailing space

With the "one of these three things" going into the Perl variable $1. In short, this regular expression isn't matching what you think its supposed to be matching. The third alternative looks like it's inteded to capture an escape sequence of some sort.

So yeah, it looks pretty messy.

Replies are listed 'Best First'.
Re:x2 This looks like someone sneezed and hit the keyboard (whitespace semantics in /x REs)
by grinder (Bishop) on Feb 03, 2004 at 08:28 UTC
    use the /x modifier on the regex, which lets you throw in whitespace for formatting without screwing up semantics

    ... with one significant caveat: spaces lose their semantics.

    The RE /foo bar/ is not the same as /foo bar/x. The latter is equivalent to /foobar/. And there is a space in the OP's RE, which will therefore be incorrect if /x is blindly applied.

    The choices are either to escape the space with a backslash (which is difficult to read, especially if the backslash-space winds up at the end of a line) or replace it with \s, which is not semantically equivalent (it can match tabs or newlines as well). There is always [\ ] but I'm not sure it's a win.

Re: Re: This looks like someone sneezed and hit the keyboard
by MCS (Monk) on Feb 03, 2004 at 16:54 UTC

    I would bet the original author probably meant to group the or's together an in that case it could be made a little faster by changing the first ( to (?: (which makes it not save the match into $1) Unless of course there is a $1 soon after the regex... in that case it was probably meant to capture it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://326140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2025-06-23 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.