Help needed with regular expressions

vodhner at cox.net vodhner at cox.net
Wed Jan 25 18:27:26 MST 2006


John Wheat wrote:
> I am seeking help to memorize, utilize, and gain
> basic proficiency with regular expressions and
> sed, grep, egrep searches and the like.

Hi, John.

I don't know any nifty tricks, but I can help a little.

First, you are not alone.  I think in English, and the
only way I can grok regexp is going left to right, a
nibble at a time.  I use them all the time, and am now
pretty good with basic regexp, but I am never ashamed
of going back to the man pages to help me decode an
expression.  And in a pinch, you can post it here for
help decoding.

Make sure you give us the whole command, and maybe
some sample data snippets, when asking for help.
Be aware that there are a few different dialects of
regexp, and you need to stay in context.

Besides what you mentioned, I often use regexp to do
searches and editing in vi or vim, and I use Perl
a lot.  So as you begin to learn this, stay in one
context where possible to avoid unnecessary
confusion.  The most basic stuff works everywhere,
but you run into annoying differences pretty quickly.

Usually on the command line you need to use some
back-slashes or another form of quoting for parens,
brackets, etc.  Just where this is needed is an added
source of confusion for me.

The example you gave worried me a little:
\^{a-r}\ has its slashes leaning the wrong way, and
I think those curly braces should be square ones.
So I'm kinda hoping you just hammered that out at random
in your frustration!  :-)  But if not, hopefully someone
here will know what that's referring to.

In case you meant /^[a-r]/ -- that would match any line
beginning with a lower case letter between "a" and "r".
In grep or egrep, -i makes matching case-insensitive.

^ at the very beginning always means the match must
begin in column 1.  Square brackets mean a set of
characters, any of which will match.  A hyphen shows
a range of letters in the character set.

Forget "man 7 regexp", that is far too abstract for
me to parse.

Try this, I just looked it up for you:
  http://sitescooper.org/tao_regexps.html

For Perl, try this:  man perlre

For another command, e.g. sed, Google for:
  regexp examples sed

Hang in there!

Vic




More information about the PLUG-discuss mailing list