100 likes | 113 Views
Regular Expressions. 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" "a" followed by zero or more characters in the range "a" to "z". Definitions.
E N D
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" "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 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 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? anbn : 8 ≥ n ≥ 0 regular abna : n is even regular a*b* regular a*b* : equal number of a's and b's non-regular
Regular Expressions in Linux 123 the string 123 [0-9] any single digit number [0-9][0-9] any two digit number [0-9]* zero or more numbers [0-9]+ one or more numbers (not always available) [a-zA-Z] any letter . any character \. the period character [.]* zero or more periods [^c] not the character c ^c 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 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 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]*" "\." "^$" "([.]*)" "o[^w]" "([^0-9]*)"