Perl Regular Expression Tutorial

標簽:perl regular   瀏覽(1328)  日期:2007-10-08
Perl Regular Expression Tutorial
 

Contents

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


Overview

Simple Regular Expressions

  gauss
  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

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

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!)

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.)

(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
  comm?a

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

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

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

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

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.
  2,\d-Dimethylbutane
  1\.\d\d\d\d\d
  a\d+z

The letter "d" in the string "\d" must be lower-case. This is because there is another metacharacter, the non-digit metacharacter, which uses the uppercase "D". The non-digit metacharacter looks like "\D" and matches any character except a digit. Thus,

  a\Dz
  \D+

Notice that in changing the "d" from lower-case to upper-case, we have reversed the meaning of the digit metacharacter. This holds true for most other metacharacters of the format backslash-letter.

There are three other metacharacters in the backslash-letter format. The first is the word metacharacter, which matches exactly one letter, one number, or the underscore character (_). It is written as "\w". It`s opposite, "\W", matches any one character except a letter, a number or the underscore. Thus,

  a\wz
  a\Wz

The whitespace metacharacter matches exactly one character of whitespace. (Whitespace is defined as spaces, tabs, newlines, or any character which would not use ink if printed on a printer.) The whitespace metacharacter looks like this: "\s". It`s opposite, which matches any character that is not whitespace, looks like this: "\S". Thus,

  a\sz
  a\Sz

  \bcomput
  \Bcomput

Note that the underscore (_) is considered a "word" character. Thus,

  super\bcomputer

There is one other metacharacter starting with a backslash, the octal metacharacter. The octal metacharacter looks like this: "\nnn", where "n" is a number from zero to seven. This is used for specifying control characters that have no typed equivalent. For example,

  \007

There are three other metacharacters that may be of use. The first is the braces metacharacter. This metacharacter follows a normal character and contains two number separated by a comma (,) and surrounded by braces ({}). It is like the star metacharacter, except the length of the string it matches must be within the minimum and maximum length specified by the two numbers in braces. Thus,

  ab{3,5}c
  .{3,5}pentane

The alternative metacharacter is represented by a vertical bar (|). It indicates an either/or behavior by separating two or more possible choices. For example:

  isopentane|cyclopentane
  \s[cmt]an\s
  2,[23]-dimethylbutane
  a[a-d]z
  textfile0[3-5]

If you wish to include a dash within brackets as one of the characters to match, instead of to denote a range, put the dash immediately before the right bracket. Thus:

  a[1234-]z
  a[1-4-]z

The bracket metacharacter can also be inverted by placing a caret (^) immediately after the left bracket. Thus,

  textfile0[^02468]
  \W[^f-h]ood\W

Note that within brackets, ordinary quoting rules do not apply and other metacharacters are not available. The only characters that can be quoted in brackets are "[", "]", and "\". Thus,

  [\[\\\]]abc

Forbidden Characters

^ (allowed within brackets)
$ (allowed within brackets)
\n
\r
\t
\f
\b
( ) (allowed within brackets. Note that if you wish to search for parentheses within text outside of brackets, you should quote the parentheses.)
\1, \2 ... \9
\B
:
!

Things To Remember

Here are some other things you should know about regular expressions.
  1. The archive search software searches only subject lines, and all articles within the same thread will also be displayed.

  2. Regular expressions should be a last resort. Because they are complex, it can be more work mastering a search than just sifting through a long list of matches (unless you`re already familiar with regular expressions).

  3. We limit the number of articles which can be shown to 200 or less. This is to minimize load on our system.

  4. The search is case insensitive; thus
      mopac
    and
      Mopac
    and
      MOPAC
    all search for the same set of strings. Each will match "mopac", "MOPAC", "Mopac", "mopaC", "MoPaC", "mOpAc" and so forth. Thus you need not worry about capitalization. (Note, however, that metacharacter must still have the proper case. This is especially important for metacharacters whose case determines whether their meaning is reversed or not.)

  5. Outside of the brackets metacharacter, you must quote parentheses, brackets and braces to get the searcher to take them literally.



-------------------------------------------------
上一篇:PERL5 Regular Expression Description 下一篇:TFTP Server - 功能強悍的 TFTP 伺服器



  
Are you Bot? How you know that?ofcz no.