Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: This looks like someone sneezed and hit the keyboard

by Roger (Parson)
on Feb 03, 2004 at 07:05 UTC ( #326136=note: print w/replies, xml ) Need Help??


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

use YAPE::Regex::Explain; $regex = qr/.*([\$#\%>~]|\@\w~\$|\\\[\\e\[0m\\\] \[0m)\s?/; print YAPE::Regex::Explain->new($regex)->explain;
__OUTPUT__ The regular expression: (?-imsx:.*([\$#%>~]|\@\w~\$|\\\[\\e\[0m\\\] \[0m)\s?) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [\$#%>~] any character of: '\$', '#', '%', '>', '~' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- \@ '@' ---------------------------------------------------------------------- \w word characters (a-z, A-Z, 0-9, _) ---------------------------------------------------------------------- ~ '~' ---------------------------------------------------------------------- \$ '$' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- \\ '\' ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- \\ '\' ---------------------------------------------------------------------- e 'e' ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- 0m '0m' ---------------------------------------------------------------------- \\ '\' ---------------------------------------------------------------------- \] ']' ---------------------------------------------------------------------- ' ' ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- 0m '0m' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- \s? whitespace (\n, \r, \t, \f, and " ") (optional (matching the most amount possible)) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

Let me show you my (colourful) command prompt... :-) which looks like: ($hostname)$fullpath>
export PS1="\[\033[1;37m\](\[\033[1;32m\]`uname -n`\[\033[1;37m\])\[\0 +33[1;36m\]\$PWD\[\033[1;37m\]>\[\033[0m\] " # ANSI colour commands # \[\033[1;37m\] => set colour to white (37) # \[\033[1;32m\] => set colour to green (32) # \[\033[1;36m\] => set colour to cyan (36) # ... # \[\033[0m\] => set colour back to normal

Replies are listed 'Best First'.
Re: Re: This looks like someone sneezed and hit the keyboard
by flyingmoose (Priest) on Feb 03, 2004 at 14:09 UTC

    In one of my first projects using Perl (I had an internship at the time), someone wanted me to parse the output of top and another Unix tool. Both used ANSI codes, so I backed away. It looks like this guy didn't back away, so I'll give him a gold star for bravery. However, he looses his gold star for not commenting his code and using an ugly regex without the /x modifier.

    It's really amazing how many people (and in good open-source programs, too) forget to add line comments here at there when they could greatly help. I am not asking for flower-box style comments, just an occasional "now we parse the ANSI terminal prompt" kind of comment here and there.

    Long story short, people who never comment their code and modules implementations need to be shot :)

      I have real trouble with remembering to use whitespace, comments, and /x in my regexs. I just don't have the habit (yet!) while writing code. Almost all the /x's that end up in my code are added after-the-fact. I'm almost ready to decide just to put an /x on all regexes (to help develop the habit), but I know that will get me strange looks.
        My regexes tend to be very basic, so, like you I don't use /x much. It would look crazy if used everywhere. However, this example is pretty ugly and needs it just for maintaince purposes. We are lucky some folks remember what ANSI escape sequences look like, as someone who didn't use ANSI codes wouldn't have a clue.
Re: Re: This looks like someone sneezed and hit the keyboard
by spartan (Pilgrim) on Feb 03, 2004 at 17:31 UTC
    wow. I'd spend all my votes to upvote this for a week if I could.
    I've always thought of regexes as a sort of black art (and still do to a certain degree), and I've always wanted something that would just explain in plain english what the heck a regex means when you read it. This could be my ticket (and possibly MANY others as well) to finally get a grip on regexes.


    Very funny Scotty... Now PLEASE beam down my PANTS!
      Have you considered reading the O'Reilly Press book "Mastering Regular Expressions" by Jeffrey E. F. Friedl? The first few chapters explain in detail how to read regexes, step by step.
Re:This looks like someone sneezed and hit the keyboard
by Anonymous Monk on Feb 05, 2004 at 09:00 UTC
    Hi, There is an article on perl.com that might be useful: http://www.perl.com/pub/a/2004/01/16/regexps.html
    Regexes are a programs. It takes time to learn new languages and that's why it might look difficult at the begining. With time and practice that kind of regex become (almost) clear.
    Using /x and commenting is very important but having the right support from the tools you use is also important. Here is a little html document that show your regex colored. I couldn't get it to show directly in this answer so you'll have to copy past :-(:
    <HTML> <HEAD> <TITLE>Smed generated dump</TITLE> </head> <body bgcolor="#FFFFFF"> <FONT color=#000000 style="BACKGROUND-COLOR: #ffffff"> <br> </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #ffffff"> <br> </FONT> <FONT color=#f00000 style="BACKGROUND-COLOR: #ffffff"> / </FONT> <FONT color=#ffff00 style="BACKGROUND-COLOR: #ff0000"> .* </FONT> <FONT color=#ffffff style="BACKGROUND-COLOR: #ff0000"> ( </FONT> <FONT color=#ffff00 style="BACKGROUND-COLOR: #643296"> [\$#\%&gt;~] < +/FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #00ff00"> | </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \@ </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #afeeee"> \w </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #f0f0ff"> ~ </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \$ </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #00ff00"> | </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \\ </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \[ </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \\ </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #f0f0ff"> e </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \[ </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #f0f0ff"> 0 </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #f0f0ff"> m </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \\ </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \] </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #f0f0ff"> </FONT> <FONT color=#ff0000 style="BACKGROUND-COLOR: #ffff00"> \[ </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #f0f0ff"> 0 </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #f0f0ff"> m </FONT> <FONT color=#ffffff style="BACKGROUND-COLOR: #ff0000"> ) </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #afeeee"> \s </FONT> <FONT color=#f00000 style="BACKGROUND-COLOR: #f0f0ff"> ? </FONT> <FONT color=#f00000 style="BACKGROUND-COLOR: #ffffff"> / </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #ffffff"> <br> </FONT> <FONT color=#000000 style="BACKGROUND-COLOR: #ffffff"> <br> </FONT> </body> </HTML>
    If your text editor supported this, you would have less problems getting in regexes. There few tools to work on regexes and they do the coloring as well.

    Cheers, Nadim (NKH).
Re: Re: This looks like someone sneezed and hit the keyboard
by Anonymous Monk on Feb 09, 2004 at 23:39 UTC
    my (colourful) command prompt... :-)

    Or, simpler, with \h instead of `uname -n`:

    export PS1="\[\033[1;37m\](\[\033[1;32m\]\u@\h\[\033[1;37m\])\[\033[1; +36m\]\$PWD\[\033[1;37m\]>\[\033[0m\] "

    which shows (username@host)path>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2023-03-25 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (63 votes). Check out past polls.

    Notices?