Article View: news.software.readers
Article #107514Regular Expressions (suck.)
From: Kevin Harris
Date: Mon, 12 Aug 2002 23:48
Date: Mon, 12 Aug 2002 23:48
216 lines
10271 bytes
10271 bytes
*** post for FREE via your newsreader at post.newsfeed.com ***
I've asked for it before.
Most of you who've read my posts know that I'm a bufoon when it comes to RE's.
So, last Friday, I put this together.
Let me know what I've missed. Really. I'm not kidding. I'd like to make a
sort of "central repository" of RE documentation.
J.E.D. - I've quoted your text but have (I think) given "proper citation".
Any problems?
Anybody else who wants to use this is free to do so.
kevin
---Regular_Expression.Notes.html---
<HTML>
<HEAD>
<TITLE>Regular Expressions for Liberal Arts Majors</TITLE>
<BODY LINK="#0000ff" VLINK="#800000" BGCOLOR="#CCCC99">
<basefont size="3" face="Arial">
<B><FONT SIZE=5><P ALIGN="CENTER">Regular Expressions for
Liberal Arts Majors</P></FONT></B>
<P>
<H2>Table of Contents</H2><BR>
i. Introduction<BR>
I. Introductory & Basic Texts<BR>
II. Perl<BR>
III. S-lang<BR>
IV. GNU Documents<BR>
V. Other Sources<BR>
<P>
<H3>i. Introduction</H3>
When I have to deal with Regular Expressions (RE's), it becomes quite
apparent that I was a Liberal Arts major in college, and not Comp. Sci. It
also becomes apparent that my parents are potty-mouths who tolerated
vulgarity from their children, but that's beside the point of this page.
<P>
RE's have been far and away the most difficult concept for me learn and
<EM>retain</EM> in my computing career - even more so than RPN and assembly
code (ugh).
<P>
I've asked-around in various Usenet groups for something like "Regular
Expressions for People who Don't Give a Shit", but to no avail. So I wrote
my own.
<P>
The idea behind this project is to collect a lot of notes and URL's from
different sources (bash, gawk, perl, s-lang docs, magazines, etc.)
pertaining to Regular Expressions and keep them in a singular point of
reference.
<P>
<H4>I. Introductory & Basic Texts</H4>
A very basic introduction to RE's can be found
<A HREF="http://etext.lib.virginia.edu/helpsheets/regex.html">here</A>.
<P>
Here's <A HREF="http://sitescooper.org/tao_regexps.html">
A Tao of Regular Expressions</A>. This is probably the best starting
point for all subsequent material.
<P>
<H4>II. Perl</H4>
First, a little word. <A HREF="http://www.perl.org">Perl</A>
is by far and away the easiest and most effective tool for text
manipulation. As such, Regular Expressions and the manipulation thereof are
central to the language.
<P>
That said, <A HREF="http://www.perldoc.com/perl5.6.1/pod/perlre.html">this</A>
is the Perl Pod (<B>P</B>lain <B>O</B>ld <B>D</B>ocumentation)
for RE's in perl. It's as good a starting point as any.
<P>
<!--
http://www.satlug.org/~kharris/pp/ch01_07.htm
http://www.satlug.org/~kharris/pp/ch02_04.htm
-->
<P>
Here's an article that appeared in "Linux Magazine" by Randal Schwartz about
<A HREF="http://www.linux-mag.com/2001-08/perl_01.html">RE's in perl</A>.
<P>
<A HREF="http://japhy.perlmonk.org">Jeff "japhy" Pinyan</A> has a great
perl-related page that has a lot about RE's.
<H4>III. S-lang</H4>
(<B>Note:</B> This is straight from <EM>slang.txt</EM> - a part of the
S-Language standard documentation available
<A HREF="http://www.s-lang.org/doc/html/slang.html">here</A>.)
<P>
<BLOCKQUOTE>
The S-Lang library includes a regular expression (RE) package that may
be used by an application embedding the library. The RE syntax should
be familiar to anyone acquainted with regular expressions. In this
section the syntax of the S-Lang regular expressions is discussed.
<P>
<B>S-Lang RE Syntax</B>
<P>
A regular expression specifies a pattern to be matched against a string, and
has the property that the contcatenation of two REs is also a RE.
<P>
The S-Lang library supports the following standard regular expressions:
<PRE>
. match any character except newline
* matches zero or more occurences of previous RE
+ matches one or more occurences of previous RE
? matches zero or one occurence of previous RE
^ matches beginning of a line
$ matches end of line
[ ... ] matches any single character between brackets.
For example, [-02468] matches `-' or any even digit.
and [-0-9a-z] matches `-' and any digit between 0 and 9
as well as letters a through z.
\< Match the beginning of a word.
\> Match the end of a word.
\( ... \)
\1, \2, ..., \9 Matches the match specified by nth \( ... \) expression.
</PRE><P>
In addition the following extensions are also supported:
<PRE>
\c turn on case-sensitivity (default)
\C turn off case-sensitivity
\d match any digit
\e match ESC char
</PRE><P>
Here are some simple examples:
<PRE>
"^int " matches the "int " at the beginning of a line.
"\<money\>" matches "money" but only if it appears as a separate word.
"^$" matches an empty line.
</PRE><P>
A more complex pattern is
<PRE>
"\(\<[a-zA-Z]+\>\)[ ]+\1\>"
</PRE><P>
which matches any word repeated consecutively. Note how the grouping
operators \( and \) are used to define the text matched by the enclosed
regular expression, and then subsequently referred to \1.
<P>
Finally, remember that when used in string literals either in the S-Lang
language or in the C language, care must be taken to "double-up" the '\'
character since both languages treat it as as an escape character.
<P>
<B>Differences between S-Lang and egrep REs</B>
<P>
There are several differences between S-Lang regular expressions and, e.g.,
egrep regular expressions.
<P>
The most notable difference is that the S-Lang regular expressions do not
support the OR operator | in expressions. This means that "a|b" or "a\|b"
do not have the meaning that they have in regular expression packages that
support egrep-style expressions.
<P>
The other main difference is that while S-Lang regular expressions support
the grouping operators \( and \), they are only used as a means of
specifying the text that is matched. That is, the expression
<P><PRE>
"@\([a-z]*\)@.*@\1@"
</PRE><P>
matches "xxx@abc@silly@abc@yyy", where the pattern \1 matches the text
enclosed by the \( and \) expressions. However, in the current imple-
mentation, the grouping operators are not used to group regular expressions
to form a single regular expression. Thus expression such as "\(hello\)*"
is not a pattern to match zero or more occurances of "hello" as it is in
e.g., egrep.
<P>
One question that comes up from time to time is why doesn't S-Lang simply
employ some posix-compatible regular expression library. The simple answer
is that, at the time of this writing, none exists that is is available
across all the platforms that the S-Lang library supports (Unix, VMS, OS/2,
win32, win16, BEOS, MSDOS, and QNX) and can be distributed under both the
GNU and Artistic licenses. It is particularly important that the library
and the interpreter support a common set of regular expressions in a
platform independent manner.
<P>
</BLOCKQUOTE>
<P>
<H4>IV. GNU Documents</H4>
Here's the <A HREF="http://www.tldp.org/LDP/abs/html/regexp.html">
Regular Expressions</A> section of the
<A HREF="http://www.freshmeat.net/projects/adv-bash-scr-guide/
?topic_id=847%2C71%2C253">Advanced Bash Scripting Guide</A>.
<P>
<A HREF="http://www.gnu.org/manual/gawk/html_node/
Expressions.html#Expressions">This</A> is from the <EM>GNU Awk Manual</EM>.
It explains how GNU's implementation of
<A HREF="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?awk">awk</A>
deals with RE's.
<P>
This is the <A HREF="http://www.satlug.org/~kharris/regex/index.html">
GNU <em>regex</em></A> library manual page. It explains a lot about
RE's.
<P>
This is the "<A HREF="http://www.gnu.org/manual/grep-2.4/html_chapter/
grep_5.html#SEC7">Regular Expressions</A>" chapter of the GNU grep manual.
<P>
<H4>V. Other Sources</H4>
<A HREF="http://www.opengroup.org/onlinepubs/7908799/xbd/re.html">
This</A> mother is huge and complex, but, by Dog, it will tell you
everything that you can imagine about RE's. Enjoy...and bring a sack lunch.<BR>
</BODY>
</HTML>
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
Technical Details
Message-ID:
<slrnalgi9q.aku.kharrisSPAMMEANDDIE@alamo.satlug.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.editors.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!ihug.co.nz!news-out.newsfeeds.com!l2!post.newsfeed.com!not-for-mailShow Full Headers
Complete Article Headers
Message-ID: <slrnalgi9q.aku.kharrisSPAMMEANDDIE@alamo.satlug.org> Subject: Regular Expressions (suck.) Date: Mon, 12 Aug 2002 18:48:11 -0500 From: Kevin Harris <kharrisSPAMMEANDDIE@alamo.DONTMESSWITHTTEXAS.satlug.org> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.editors.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!ihug.co.nz!news-out.newsfeeds.com!l2!post.newsfeed.com!not-for-mail Newsgroups: comp.editors,comp.mail.mutt,news.software.readers Followup-To: comp.editors User-Agent: slrn/0.9.7.4 (Linux) NNTP-Posting-Host: 207.235.6.99 X-Original-NNTP-Posting-Host: 207.235.6.99 X-Trace: post.newsfeed.com 1029195649 207.235.6.99 (12 Aug 2002 18:40:49 -0500) Organization: Newsfeeds.com http://www.newsfeed.com 100,000+ UNCENSORED Newsgroups. The #1 Usenet Service on the Planet! Lines: 215 X-Comments: This message was posted through Newsfeed.com's, FREE publicly accessible Usenet Server - "post.newsfeeds.com" X-Comments2: IMPORTANT: Newsfeed.com does not condone, nor support, spam or any illegal or copyrighted postings. X-Comments3: IMPORTANT: Under NO circumstances will postings containing illegal or copyrighted material through this service be tolerated!! X-Report: Please report illegal or inappropriate use to <abuse@newsfeeds.com> X-Abuse-Info: Please be sure to forward a copy of ALL headers, INCLUDING the body (DO NOT SEND ATTACHMENTS)