ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 正则表达式

正则表达式

原创 Linux操作系统 作者:syzxlyx_cu 时间:2009-09-28 12:00:41 0 删除 编辑

Table 3.1. Regular Expression Metacharacters Metacharacter
 Function
 Example
 What It Matches
 
^
 Beginning-of-line anchor
 /^love/
 Matches all lines beginning with love
 
$
 End-of-line anchor
 /love$/
 Matches all lines ending with love
 
.
 Matches one character
 /l..e/
 Matches lines containing an l, followed by two characters, followed by an e
 
*
 Matches zero or more of the preceding characters
 / *love/
 Matches lines with zero or more spaces, followed by the pattern love
 
[ ]
 Matches one in the set
 /[Ll]ove/
 Matches lines containing love or Love
 
[x-y]
 Matches one character within a range in the set
 /[A-Z]ove/
 Matches letters from A through Z followed by ove
 
[^ ]
 Matches one character not in the set
 /[^A-Z]/
 Matches any character not in the range between A and Z
 
\
 Used to escape a metacharacter
 /love\./
 Matches lines containing love, followed by a literal period; Normally the period matches one of any character
 
Additional Metacharacters Supported by Many UNIX/Linux Programs That Use RE Metacharacters
 
\<
 Beginning-of-word anchor
 /\ Matches lines containing a word that begins with love (supported by vi and grep)
 
\>
 End-of-word anchor
 /love\>/
 Matches lines containing a word that ends with love (supported by vi and grep)
 
\(..\)
 Tags match characters to be used later
 /\(love\)able \1er/
 May use up to nine tags, starting with the first tag at the leftmost part of the pattern. For example, the pattern love is saved as tag 1, to be referenced later as \1. In this example, the search pattern consists of lovable followed by lover (supported by sed, vi, and grep)
 
x\{m\} or x\{m,\} or x\{m,n\}
 Repetition of character x, m times, at least m times, at least m and not more than n times[a]
 o\{5,10\}
 Matches if line contains between 5 and 10 consecutive occurrences of the letter o (supported by vi and grep)
 

 


[a] Not dependable on all versions of UNIX/Linux or all pattern-matching utilities; usually works with vi and grep.

Assuming that you know how the vi editor works, each metacharacter is described in terms of the vi search string. In the following examples, characters are highlighted to demonstrate what vi will find in its search

 

 

Note: The line numbers are NOT part of the text file. The vertical bars mark the left and

 right margins.

   ---------------------------------------------------------------

1  |Christian Scott lives here and will put on a Christmas party.|

2  |There are around 30 to 35 people invited.                    |

3  |They are:                                                    |

4  |                                                          Tom|

5  |Dan                                                          |

6  |   Rhonda Savage                                             |

7  |Nicky and Kimberly.                                          |

8  |Steve, Suzanne, Ginger and Larry.                            |

   ---------------------------------------------------------------

 


EXPLANATION

/^[A-Z]..$/ Will find all lines beginning with a capital letter, followed by two of any character, followed by a newline. Will find Dan on line 5.

/^[A-Z][a-z ]*3[0-5]/ Will find all lines beginning with an uppercase letter, followed by zero or more lowercase letters or spaces, followed by the number 3 and another number between 0 and 5. Will find line 2.

/[a-z]*\ ./ Will find lines containing zero or more lowercase letters, followed by a literal period. Will find lines 1, 2, 7, and 8.

/^ *[A-Z][a-z][a-z]$/ Will find a line that begins with zero or more spaces (tabs do not count as spaces), followed by an uppercase letter, two lowercase letters, and a newline. Will find Tom on line 4 and Dan on line 5.

/^[A-Za-z]*[^,][A-Za-z]*$/ Will find a line that begins with zero or more uppercase and/or lowercase letters, followed by a noncomma, followed by zero or more upper- or lowercase letters and a newline. Will find line 5.

3.2.1 More Regular Expression Metacharacters
The following metacharacters are not necessarily portable across all utilities using regular expressions, but can be used in the vi editor and some versions of sed and grep. There is an extended set of metacharacters available with egrep and awk, which will be discussed in later sections.

Example 3.10.

    (Beginning-of-word (\<) and end-of-word (\>) anchors)

    % vi textfile

    -------------------------------------------------------------

    Unusual occurrences happened at the fair.

--> Patty won fourth place in the 50 yard dash square and fair.

    Occurrences like this are rare.

    The winning ticket is 55222.

    The ticket I got is 54333 and Dee got 55544.

    Guy fell down while running around the south bend in his last event.

    ~

    ~

    ~

    /\/

    -------------------------------------------------------------

 


EXPLANATION

Will find the word fourth on each line. The \< is the beginning-of-word anchor and the \ > is the end-of-word anchor. A word can be separated by spaces, end in punctuation, start at the beginning of a line, end at the end of a line, and so forth.

Example 3.11.

    % vi textfile

    -------------------------------------------------------------

    Unusual occurrences happened at the fair.

--> Patty won fourth place in the 50 yard dash square and fair.

    Occurrences like this are rare.

    The winning ticket is 55222.

    The ticket I got is 54333 and Dee got 55544.

--> Guy fell down while running around the south bend in his last event.

    ~

    ~

    ~

    /\/

    -------------------------------------------------------------

 


EXPLANATION

Will find any word (or group of words) beginning with an f, followed by zero or more of any character (.*), and a string ending with th.
Example 3.14.

(Repetition of patterns ( \{n\} ))

    % vi textfile

    -------------------------------------------

    Unusual occurrences happened at the fair.

    Patty won fourth place in the 50 yard dash square and fair.

    Occurrences like this are rare.

--> The winning ticket is 55222.

    The ticket I got is 54333 and Dee got 55544.

    Guy fell down while running around the south bend in his last

    event.

    ~

    ~

    ~

    ~

1   /5\{2\}2\{3\}\./

    -------------------------------------------------------------

 


EXPLANATION

Searches for lines containing two occurrences of the number 5, followed by three occurrences of the number 2, followed by a literal period.


1   :1,$s/\([0o]ccur\)ence/\1rence/

    -------------------------------------------------------------

 

 

    % vi textfile (After substitution)

    -------------------------------------------------

--> Unusual occurrences happened at the fair.

    Patty won fourth place in the 50 yard dash square and fair.

--> Occurrences like this are rare.

    The winning ticket is 55222.

    The ticket I got is 54333 and Dee got 55544.

    Guy fell down while running around the south bend in his last event.

    ~

    ~

    ~

    -------------------------------------------------------------

 


EXPLANATION

The editor searches for the entire string occurence (intentionally misspelled) or Occurrence and if found, the pattern portion enclosed in parentheses is tagged (i.e., either occur or Occur is tagged). Because this is the first pattern tagged, it is called tag 1. The pattern is stored in a memory register called register 1. On the replacement side, the contents of the register are replaced for \1 and the rest of the word, rence, is appended to it. We started with occurence and ended up with occurrence

 

Occurrences like this are rare.

    The winning ticket is 55222.

    The ticket I got is 54333 and Dee got 55544.

    Guy fell down while running around the south bend in his last event.

    ~

    ~

    ~
:s/\(square\)and \(fair\)/\2 and \1/
    -------------------------------------------------------------

 


EXPLANATION

The editor searches for the regular expression square and fair, and tags square as 1 and fair as 2. On the replacement side, the contents of register 2 are substituted for \2 and the contents of register 1 are substituted for \1.

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20542911/viewspace-615777/,如需转载,请注明出处,否则将追究法律责任。

下一篇: cshell语法
请登录后发表评论 登录
全部评论

注册时间:2009-09-28

  • 博文量
    125
  • 访问量
    124096