Article View: news.software.readers
Article #42729Re: [slrn] those darned regexps again (long)
From: Dawn Owens-Nicholson
Date: Sat, 15 Aug 1998 00:00
Date: Sat, 15 Aug 1998 00:00
124 lines
7221 bytes
7221 bytes
Warning: the following article is lengthy and might not be interesting to
people who want to read general slrn discussion. It's late as I type this,
but I think it's all technically correct and understandable with a small
level of regex experience.
Ian Zimmerman <itz@kronstadt.boxmail.com> wrote:
> I thought I made it clear in my article that I wasn't just looking for
> something that works, but also for the reason _why_ it works and what I'd
> tried doesn't.
The SCORE_FAQ explains why the regular expressions listed therein work. I'll
try explaining why yours don't work. For more info, I recommend Jeffrey
E.F. Friedl's _Mastering Regular Expressions_. I've read his book and I
think it's an excellent reference (even if it is largely for perl regular
expressions).
Getting back to your regex situation, you really only have two regular
expressions here (the third is the same as the first minus the attempt to
match the header name), and both have two major problems:
(1) "*" matches more (but is satisfied with less) than most people think.
The asterisk metacharacter means 0 or more repetitions of the previous
character (also known as "\{0,\}"); it is okay for the asterisk to match 0
times. It will /try/ to match more, but if it has to give up everything
it matched so some other part of the regex can match, that's okay. This
means "[^a-z]*" will match *everything*, even the null string.
(2) Slrn scorefile regular expressions are case-insensitive by default,
just like using grep with the 'i' option. "[^a-z]" (no lowercase alpha)
is seen as "[^a-zA-Z]" (no alpha). The score documentation and SCORE_FAQ
talk about "\c" (turn on case sensitivity) and "\C" (turn off case
sensitivity). Just to be crystal clear on these case-sensitivity
switches: you don't need to switch case sensitivity back on in another
regex. Each regex in your scorefile is case-insensitive unless you say
otherwise. You need "\C" in case you want to say something like:
Subject: \cRe: \Cwacky warehouse mall
which treats "Re: " in a case-sensitive manner ("re: ", "rE: " and "RE: "
are not matched) and the rest of the line in a case-insensitive manner.
I'll assume you wanted to match all-caps Subject: headers and kill them.
From <slrn6t8s6m.uoq.itz@kronstadt.rahul.net>:
> Subject: ^[^:]+: [^a-z]*$
I'm going to ignore the "^[^:]+: " part because it appears that you're
trying to match the header name (and header-content separator) here, which
you don't do in slrn scorefiles.
In addition to the two issues noted above, "[^a-z]*$" is anchored at the end
of the line. Because of the lack of "\c", "[^a-z]*$" is treated like
"[^a-zA-Z]*$". So, you're asking for Subject: headers that have 0 or more
non-alphabetic characters at the end of the line. If there are 0 non-alpha
characters at the end of the line, it's a match; if there are 200 non-alpha
characters at the end of the line, it's a match. But really, this isn't
getting at what you're interested in matching.
For example, if the Subject: header were "Subject: B" and you used "Subject:
[^a-z]*$", the regex engine would treat it the same as "Subject:
[^a-zA-Z]*$" and step right past the ``B'', get the null string between the
``B'' and the end of the line to match "[^a-zA-Z]*" then match the end of
the line. Result: a match!
> Subject: ^[^a-z]*$
This is largely the same as the previous regex, so the same two main
problems apply, except this regex is also anchored to the beginning of the
Subject: header content. This additional anchoring means the *entire*
Subject: header content has to have 0 or more non-alphabetic characters, as
opposed to just having any number of non-alpha characters at the end of the
line.
Put another way, when you use "^" and "$", what lies between them describes
the *entire* Subject: header content. Therefore, if there's *one* alpha
character in the header content, there is no match ("[^a-zA-Z]*" wants
*NON*-alpha characters).
For example, if the Subject: header were "Subject: B" and you used "Subject:
^[^a-z]*$", you'd essentially have "Subject: ^[^a-zA-Z]*$". The regex
engine matches the first caret (beginning of line anchor) and tries to match
the character class "[^a-z]*" (0 or more non-alpha characters). There are 0
non-alpha characters between the beginning of line and the ``B'', so
"[^a-z]*" (a.k.a. "[^a-zA-Z]*") matches. Moving on, the engine tries to
match the "$" (end of line anchor) and it can't because the end of line
doesn't follow the null string matched by the character class "[^a-z]*";
there's ``B'' in the way. This `B'' needs to be dealt with in the regex,
but there's no more regex left. Result: not a match.
> Subject: [^a-z]*$
This is a repeat of the first regex.
Neither regex works because neither addresses the issue at hand. The first
regex (anchored at the end) matches "Subject: BLAH" but also matches
"Subject: blah", something you don't want to happen. The second regex
(anchored at both ends) matches neither "Subject: BLAH" nor "Subject: blah"
so it is obviously useless at identifying the Subject: headers you want.
Neither regex is targeting the issue of excluding all-caps headers because
neither is focused on matching all-caps headers or looking for a lowercase
alpha (to declare a non-match).
Before I move to the next topic in your follow-up, I want to suggest you
alter the last SCORE_FAQ scorefile entry in the following manner: Use
"Score: =-9999" instead of "Score: -9999". I think it's a good idea to not
use -9999 (without the equal sign) to kill articles because the equal sign
insures a killed article as opposed to merely subtracting 9,999 points from
the article's score. After all, the article is only killed if it ends up
with kill_score points (-9,999 by default). The equal sign stops processing
and assigns whatever score follows.
> Anyways, because I prefer _not_ to receive email answers to Usenet queries
> (isn't that still the default?) it did its job this time :-)
I don't know if you consider copies of follow-ups e-mail answers, but to
attempt to observe your wishes, I will not send you a copy of this post. You
should consider adding a header to make your position on courtesy copies
clear (I've forgotten what the header name and content is, but I'm sure
someone on n.s.r knows) since you dislike e-mail responses to Usenet
queries.
Hope this helps!
Technical Details
Message-ID:
<slrn6tbmdu.1te.bluemoon@ux6.cso.uiuc.edu>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!not-for-mailReferences:
<slrn6t8s6m.uoq.itz@kronstadt.rahul.net> <slrn6t94lm.bro.bluemoon@ux6.cso.uiuc.edu> <slrn6t9gp9.3tn.itz@kronstadt.rahul.net>Show Full Headers
Complete Article Headers
Message-ID: <slrn6tbmdu.1te.bluemoon@ux6.cso.uiuc.edu> Subject: Re: [slrn] those darned regexps again (long) Date: Sat, 15 Aug 1998 00:00:00 +0000 From: bluemoon@ux6.cso.uiuc.edu (Dawn Owens-Nicholson) References: <slrn6t8s6m.uoq.itz@kronstadt.rahul.net> <slrn6t94lm.bro.bluemoon@ux6.cso.uiuc.edu> <slrn6t9gp9.3tn.itz@kronstadt.rahul.net> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!not-for-mail X-Deja-AN: 381474444 Organization: University of Illinois at Urbana-Champaign Newsgroups: news.software.readers