10
Pattern Matching CSCI N321 – System and Network Administration

Pattern Matching

  • Upload
    kovit

  • View
    22

  • Download
    2

Embed Size (px)

DESCRIPTION

Pattern Matching. CSCI N321 – System and Network Administration. Section Overview. Regular Expressions Matching Characters Grouping/Repetition Characters Tagging (Buffering) Character Classes. CQU 85321 System Administration Course. References. Textbook Chapter 7. Lectures 1999 #7. - PowerPoint PPT Presentation

Citation preview

Page 1: Pattern Matching

Pattern Matching

CSCI N321 – System and Network Administration

Page 2: Pattern Matching

Section Overview

Regular Expressions

Matching Characters

Grouping/Repetition Characters

Tagging (Buffering)

Character Classes

Page 3: Pattern Matching

References

CQU 85321 System Administration Course

TextbookChapter 7

Lectures1999 #7

Page 4: Pattern Matching

Pattern Matching

Powerful tool to extract/modify file dataRegular Expressions Special “string” used to match patterns Special (Meta) Characters

Matching Grouping/Repeating Tagging (buffering)

Use with ASCII files

Page 5: Pattern Matching

Programs supporting REs

Extracting information grep egrep

Modifying information sed & awk ex & vi

Filename globbing versus REs

Page 6: Pattern Matching

Matching CharactersCharacterCharacter MatchesMatches

c Any single character (other than a RE special character)

\Removes the special meaning from s RE special character (escape)

. Matches any single character

^ Matched start of line

$ Matches end of line

[chars] Matches any one character in chars

[^chars] Matches any one character not in chars

Page 7: Pattern Matching

Grouping/Repetition Characters

CharacterCharacter MatchesMatches

* 0 or more of previous RE

+ 1 or more of previous RE

? 0 or 1 occurrence of previous RE

\{n\} Exactly n occurrences of previous RE

\{n,\} At least n occurrences of previous RE

\{n,m\} between n & m occurrences of previous RE

| One of two different REs (alternation)

( ) Groups a collection of REs

Page 8: Pattern Matching

Tagging (Buffering)

Takes the output from one RE and uses it again laterREs to reuse are in \( \) - TaggingBuffers are referenced by \n where n is the register number

$ egrep ‘([a-z])([a-z])\2\1’ /usr/share/dict/words

Would return words like attach, better, and common.

Page 9: Pattern Matching

RE Character Classes

LiteralLiteral MatchesMatches

[:alnum:] Any alphabetical or numeric character

[:cntrl:] Any control character

[:lower:] Any lowercase letter

[:space:]Space, form-feed, newline, carriage return, horizontal tab, vertical tab

[:alpha:] Any alphabetical character

[:digit:] Any numeric character

Page 10: Pattern Matching

RE Character Classes (Con’t)

LiteralLiteral MatchesMatches

[:print:] Any printable character

[:upper:] Any uppercase character

[:blank:] Blank, usually space and tab

[:graph:] Any printable character except space

[:punct:]Any printable character which is not a space or an alphanumeric character

[:xdigit:] any hexadecimal digit ([0-9a-eA-E])