Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Any given program, when running, is obsolete.


computers / alt.usenet.offline-reader.forte-agent / Agent Filtering: Perl Regular Expression Tutorial

SubjectAuthor
o Agent Filtering: Perl Regular Expression Tutorialsam

1
Agent Filtering: Perl Regular Expression Tutorial

<imbjth514dcag92c7efe41rldogtm6ekpm@4ax.com>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=4748&group=alt.usenet.offline-reader.forte-agent#4748

  copy link   Newsgroups: alt.usenet.offline-reader.forte-agent
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: sam@invalid.com
Newsgroups: alt.usenet.offline-reader.forte-agent
Subject: Agent Filtering: Perl Regular Expression Tutorial
Date: Tue, 31 Jan 2023 18:15:10 -0600
Organization: A noiseless patient Spider
Lines: 478
Message-ID: <imbjth514dcag92c7efe41rldogtm6ekpm@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: reader01.eternal-september.org; posting-host="0cf6625eb7ffccad434521ebb98a805c";
logging-data="43542"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/KkcCQjBSKLKZER3WYghJT"
Cancel-Lock: sha1:EnNIE9EvbdqvH+q0a94UxgFIDYc=
X-No-Archive: yes
X-Newsreader: Forte Agent 1.93/32.576 English (American)
 by: sam@invalid.com - Wed, 1 Feb 2023 00:15 UTC

Perl Regular Expression Tutorial

Contents

1. Overview 2. Simple Regular Expressions 3. Metacharacters 4.
Forbidden Characters 5. Things To Remember

Overview A regular expression is a string of characters which
tells the searcher which string (or strings) you are looking
for. The following explains the format of regular expressions in
detail. If you are familiar with Perl, you already know the
syntax. If you are familiar with Unix, you should know that
there are subtle differences between Perl's regular expressions
and Unix' regular expressions. Simple Regular Expressions In its
simplest form, a regular expression is just a word or phrase to
search for. For example,

gauss

would match any subject with the string "gauss" in it, or which
mentioned the word "gauss" in the subject line. Thus, subjects
with "gauss", "gaussian" or "degauss" would all be matched, as
would a subject containing the phrases "de-gauss the monitor" or
"gaussian elimination." Here are some more examples:

carbon

Finds any subject with the string "carbon" in its name, or which
mentions carbon (or carbonization or hydrocarbons or
carbon-based life forms) in the subject line.

hydro

Finds any subject with the string "hydro" in its name or
contents. Subjects with "hydro", "hydrogen" or "hydrodynamics"
are found, as well as subjects containing the words "hydroplane"
or "hydroelectric".

oxy

Finds any subject with the string "oxy" in the subject line.
This could be used to find subjects on oxygen, boxy houses or
oxymorons.

top ten

Note that spaces may be part of the regular expression. The
above expression could be used to find top ten lists. (Note that
they would also find articles on how to stop tension.)

Metacharacters Some characters have a special meaning to the
searcher. These characters are called metacharacters. Although
they may seem confusing at first, they add a great deal of
flexibility and convenience to the searcher.

The period (.) is a commonly used metacharacter. It matches
exactly one character, regardless of what the character is. For
example, the regular expression:

2,.-Dimethylbutane

will match "2,2-Dimethylbutane" and "2,3-Dimethylbutane". Note
that the period matches exactly one character-- it will not
match a string of characters, nor will it match the null string.
Thus, "2,200-Dimethylbutane" and "2,-Dimenthylbutane" will not
be matched by the above regular expression.

But what if you wanted to search for a string containing a
period? For example, suppose we wished to search for references
to pi. The following regular expression would not work:

3.14 (THIS IS WRONG!)

This would indeed match "3.14", but it would also match "3514",
"3f14", or even "3+14". In short, any string of the form "3x14",
where x is any character, would be matched by the regular
expression above.

To get around this, we introduce a second metacharacter, the
backslash (\). The backslash can be used to indicate that the
character immediately to its right is to be taken literally.
Thus, to search for the string "3.14", we would use:

3\.14 (This will work.)

This is called "quoting". We would say that the period in the
regular expression above has been quoted. In general, whenever
the backslash is placed before a metacharacter, the searcher
treats the metacharacter literally rather than invoking its
special meaning.

(Unfortunately, the backslash is used for other things besides
quoting metacharacters. Many "normal" characters take on special
meanings when preceded by a backslash. The rule of thumb is,
quoting a metacharacter turns it into a normal character, and
quoting a normal character may turn it into a metacharacter.)

Let's look at some more common metacharacters. We consider first
the question mark (?). The question mark indicates that the
character immediately preceding it either zero times or one
time. Thus

m?ethane

would match either "ethane" or "methane". Similarly,

comm?a

would match either "coma" or "comma".

Another metacharacter is the star (*). This indicates that the
character immediately to its left may be repeated any number of
times, including zero. Thus

ab*c

would match "ac", "abc", "abbc", "abbbc", "abbbbbbbbc", and any
string that starts with an "a", is followed by a sequence of
"b"'s, and ends with a "c".

The plus (+) metacharacter indicates that the character
immediately preceding it may be repeated one or more times. It
is just like the star metacharacter, except it doesn't match the
null string. Thus

ab+c

would not match "ac", but it would match "abc", "abbc", "abbbc",
"abbbbbbbbc" and so on.

Metacharacters may be combined. A common combination includes
the period and star metacharacters, with the star immediately
following the period. This is used to match an arbitrary string
of any length, including the null string. For example:

cyclo.*ane

would match "cyclodecane", "cyclohexane" and even "cyclones
drive me insane." Any string that starts with "cyclo", is
followed by an arbitrary string, and ends with "ane" will be
matched. Note that the null string will be matched by the
period-star pair; thus, "cycloane" would be matche by the above
expression.

If you wanted to search for articles on cyclodecane and
cyclohexane, but didn't want to match articles about how
cyclones drive one insane, you could string together three
periods, as follows:

cyclo...ane

This would match "cyclodecane" and "cyclohexane", but would not
match "cyclones drive me insane." Only strings eleven characters
long which start with "cyclo" and end with "ane" will be
matched. (Note that "cyclopentane" would not be matched,
however, since cyclopentane has twelve characters, not eleven.)

Here are some more examples. These involve the backslash. Note
that the placement of backslash is important.

a\.*z

Matches any string starting with "a", followed by a series of
periods (including the "series" of length zero), and terminated
by "z". Thus, "az", "a.z", "a..z", "a...z" and so forth are all
matched.

a.\*z

(Note that the backslash and period are reversed in this regular
expression.)

Matches any string starting with an "a", followed by one
arbitrary character, and terminated with "*z". Thus, "ag*z",
"a5*z" and "a@*z" are all matched. Only strings of length four,
where the first character is "a", the third "*", and the fourth
"z", are matched.

a\++z

Matches any string starting with "a", followed by a series of
plus signs, and terminated by "z". There must be at least one
plus sign between the "a" and the "z". Thus, "az" is not
matched, but "a+z", "a++z", "a+++z", etc. will be matched.

a\+\+z

Matches only the string "a++z".

a+\+z

Matches any string starting with a series of "a"'s, followed by
a single plus sign and ending with a "z". There must be at least
one "a" at the start of the string. Thus "a+z", "aa+z", "aaa+z"
and so on will match, but "+z" will not.

a.?e

Matches "ace", "ale", "axe" and any other three-character string
beginning with "a" and ending with "e"; will also match "ae".

a\.?e

Matches "ae" and "a.e". No other string is matched.

a.\?e

Matches any four-character string starting with "a" and ending
with "?e". Thus, "ad?e", "a1?e" and "a%?e" will all be matched.

a\.\?e

Matches only "a.?e" and nothing else.

Earlier it was mentioned that the backslash can turn ordinary
characters into metacharacters, as well as the other way around.
One such use of this is the digit metacharacter, which is
invoked by following a backslash with a lower-case "d", like
this: "\d". The "d" must be lower case, for reasons explained
later. The digit metacharacter matches exactly one digit; that
is, exactly one occurence of "0", "1", "2", "3", "4", "5", "6",
"7", "8" or "9". For example, the regular expression:

2,\d-Dimethylbutane

would match "2,2-Dimethylbutane", "2,3-Dimethylbutane" and so
forth. Similarly,

1\.\d\d\d\d\d

would match any six-digit floating-point number from 1.00000 to
1.99999 inclusive. We could combine the digit metacharacter with
other metacharacters; for instance,


Click here to read the complete article
1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor