Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expressions.

Similar presentations


Presentation on theme: "Regular Expressions."— Presentation transcript:

1 Regular Expressions

2 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"

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

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

5 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

6 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

7 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

8 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

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


Download ppt "Regular Expressions."

Similar presentations


Ads by Google