1 / 22

i206: Lecture 18: Regular Expressions

i206: Lecture 18: Regular Expressions. Marti Hearst Spring 2012. Distributed Systems. Security. Formal Languages. Cryptography. Network. Standards & Protocols. Inter-process Communication. Methodologies/ Tools. Principles. Operating System. Formal models. Application. Design.

weatherly
Download Presentation

i206: Lecture 18: 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. i206: Lecture 18:Regular Expressions Marti Hearst Spring 2012

  2. Distributed Systems Security FormalLanguages Cryptography Network Standards & Protocols Inter-process Communication Methodologies/ Tools Principles OperatingSystem Formal models Application Design Process I/O Finite automata regex Algorithms Program Analysis Memory hierarchy ALUs, Registers, Program Counter, Instruction Register Memory Big-O Compiler/ Interpreter Assembly Instructions Data Structures Register, Cache Main Memory, Secondary Storage Searching, sorting, etc. Machine Instructions CPU Op-code, operands Instruction set arch Data storage Stacks, queues, maps, trees, graphs, … Circuits Lossless v. lossy Info entropy & Huffman code Decimal, Hexadecimal, Binary Adders, decoders, Memory latches, ALUs, etc. Gates Data compression Number Systems Data AND, OR, NOT, XOR, NAND, NOR, etc. Boolean Logic Numbers, text, audio, video, image, … Truth table Venn Diagram DeMorgan’s Law Data Representation Binary Numbers Bits & Bytes

  3. Theories of Computation • What are the fundamental capabilities and limitations of computers? • Complexity theory: what makes some problems computationally hard and others easy? • Automata theory: definitions and properties of mathematical models of computation • Computability theory: what makes some problems solvable and others unsolvable?

  4. Finite Automata • Also known as finite state automata (FSA) or finite state machine (FSM) • A simple mathematical model of a computer • Applications: hardware design, compiler design, text processing

  5. A First Example • Touch-less hand-dryer DRYER OFF DRYER ON

  6. A First Example • Touch-less hand-dryer hand hand hand DRYER OFF DRYER ON hand

  7. x Finite Automata State Graphs • A state • The start state • An accepting state • A transition

  8. Finite Automata • Transition: s1xs2 • In state s1 on input “x” go to state s2 • If end of input • If in accepting state => accept • Else => reject • If no transition possible => reject

  9. letter | digit letter S A Language of a FA • Language of finite automaton M: set of all strings accepted by M • Example: • Which of the following are in the language? • x, tmp2, 123, a?, 2apples • A language is called a regular language if it is recognized by some finite automaton

  10. digit B digit digit + S A - Example • What is the language of this FA?

  11. Regular Expressions • Regular expressions are used to describe regular languages • Arithmetic expression example: (8+2)*3 • Regular expression example: (\+|-)?[0-9]+

  12. digit B digit digit + S A - Example • What is the language of this FA? • Regular expression: (\+|-)?[0-9]+

  13. Three Equivalent Representations Regular expressions Each can describe the others Regular languages Finite automata • Theorem: • For every regular expression, there is a deterministic finite-state automaton that defines the same language, and vice versa. Adapted from Jurafsky & Martin 2000

  14. Regex Rules • ? Zero or one occurrences of the preceding character/regex woodchucks? “how much wood does a woodchuck chuck?” behaviou?r “behaviour is the British spelling of behavior” • * Zero or more occurrences of the preceding character/regex baa* ba, baa, baaa, baaaa … ba* b, ba, baa, baaa, baaaa … [ab]* , a, b, ab, ba, baaa, aaabbb, … [0-9][0-9]* any positive integer, or zero cat.*cat A string where “cat” appears twice anywhere • + One or more occurrences of the preceding character/regex ba+ ba, baa, baaa, baaaa … • {n} Exactly n occurrences of the preceding character/regex ba{3} baaa

  15. Regex Rules • * is greedy: <.*> <a href=“index.html”>Home</a> • Lazy (non-greedy) quantifier: <.*?> <a href=“index.html”>Home</a>

  16. Regex Rules • [] Disjunction (Union) [wW]ood “how much wood does a Woodchuck chuck?” [abcd]* “you are a good programmer” [A-Za-z0-9] (any letter or digit) [A-Za-z]* (any letter sequence) • | Disjunction (Union) (cats?|dogs?)+ “It’s raining cats and a dog.” • ( ) Grouping (gupp(y|ies))* “His guppy is the king of guppies.”

  17. Regex Rules • ^ $ \b Anchors (start/end of input string; word boundary) ^The “The cat in the hat.” ^The end\.$ “The end.” ^The .* end\.$ “The bitter end.” (the)* “I saw him the other day.” (\bthe\b)* “I saw him the other day.” • Special rule: when ^ is FIRST WITHIN BRACKETS it means NOT [^A-Z]* (anything not an upper case letter)

  18. Regex Rules • \ Escape characters \. “The + and \ characters are missing.” \+ “The + and \ characters are missing.” \\ “The + and \ characters are missing.”

  19. Operator Precedence • What is the difference? • [a-z][a-z]|[0-9]* • [a-z]([a-z]|[0-9])*

  20. Regex for Dollars • No commas \$[0-9]+(\.[0-9][0-9])? • With commas \$[0-9][0-9]?[0-9]?(,[0-9][0-9][0-9])*(\.[0-9][0-9])? • With or without commas \$[0-9][0-9]?[0-9]?((,[0-9][0-9][0-9])*| [0-9]*) (\.[0-9][0-9])?

  21. Regex in Python • import re • result = re.search(pattern, string) • result = re.findall(pattern, string) • result = re.match(pattern, string) • Python documentation on regular expressions • http://docs.python.org/release/3.1.3/library/re.html • Some useful flags like IGNORECASE, MULTILINE, DOTALL, VERBOSE

More Related