Regular Expressions simple examples and matches the word

  • Slides: 9
Download presentation
Regular Expressions

Regular Expressions

simple examples "and" matches the word "and" a. . matches any three letter word

simple examples "and" matches the word "and" a. . matches any three letter word starting with the letter "a", eg "and" & "are" a[a-z]* matches any word beginning with "a" followed by zero or more characters in the range "a" to "z"

Definitions • All regular expressions can be specified with a FA. • Regular expressions

Definitions • All regular expressions can be specified with a FA. • Regular expressions specify Regular Languages. • All regular languages can be expressed via a regular expression.

Example • anbn is not regular. • This pattern requires the machine to count

Example • anbn is not regular. • This pattern requires the machine to count the number of a's, or push them onto a stack, or some other extra storage not available via a Finite Automata. • Thus, since we can't build a FA to match this pattern, this regular expression is not regular.

Regular or Non-Regular? a nb n : 8 ≥ n ≥ 0 regular abna

Regular or Non-Regular? a nb n : 8 ≥ n ≥ 0 regular abna : n is even regular a*b* : equal number of a's and b's non-regular

Regular Expressions in Linux 123 [0 -9][0 -9]* [0 -9]+ [a-z. A-Z]. . [.

Regular Expressions in Linux 123 [0 -9][0 -9]* [0 -9]+ [a-z. A-Z]. . [. ]* [^c] ^c $ the string 123 any single digit number any two digit number zero or more numbers one or more numbers (not always available) any letter any character the period character zero or more periods not the character c at start of a line end of line

Examples [A-Z][a-z]* capitalized words ^[A-Z][a-z]* capitalized words at the start of a line a[ab]*b

Examples [A-Z][a-z]* capitalized words ^[A-Z][a-z]* capitalized words at the start of a line a[ab]*b strings of a's and b's that start with an a and end with a b ^[0 -9]+$ lines that contain exactly one integer

More Examples [a-z]o[^w] matches "for" and "too", but not "now" (. *) parenthesis around

More Examples [a-z]o[^w] matches "for" and "too", but not "now" (. *) parenthesis around zero or more characters (. . *) parenthesis around one or more characters ([^0 -9]*) parenthesis around anything, or nothing, except numbers

grep echoes lines containing that pattern usage : grep pattern file "[0 -9]*" ".

grep echoes lines containing that pattern usage : grep pattern file "[0 -9]*" ". " "^$" "([. ]*)" "o[^w]" "([^0 -9]*)"