1 / 9

Regular Expressions

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.

mcraft
Download Presentation

Regular Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 usage : grep pattern file "[0-9]*" "\." "^$" "([.]*)" "o[^w]" "([^0-9]*)"

More Related