cs 10 problem solving via object oriented programming
play

CS 10: Problem solving via Object Oriented Programming - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 19 PaHern Matching Agenda 1. Regular


  1. CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡ Winter ¡2017 ¡ ¡ Tim ¡Pierson ¡ 260 ¡(255) ¡Sudikoff ¡ Day ¡19 ¡– ¡PaHern ¡Matching ¡

  2. Agenda ¡ 1. Regular ¡expressions ¡ 2. Finite ¡automata ¡ 3. ValidaSng ¡input ¡ ¡ 4. Modeling ¡a ¡complex ¡system ¡ 2 ¡

  3. SomeSmes ¡it ¡is ¡useful ¡to ¡be ¡able ¡to ¡detect ¡ or ¡require ¡paHerns ¡ Email ¡addresses ¡follow ¡a ¡paHern: ¡ ¡ mailbox@domain.TLD ¡ ¡ ¡ example: ¡tjp@cs.dartmouth.edu ¡ ¡ We ¡can ¡specify ¡a ¡paHern ¡or ¡rules ¡for ¡email ¡addresses: ¡ ¡ <characters> ¡@ ¡<characters>.<com ¡| ¡edu ¡| ¡org ¡| ¡…> ¡ One ¡or ¡ One ¡or ¡more ¡ Followed ¡by ¡one ¡of ¡a ¡set ¡ more ¡ characters ¡ predefined ¡of ¡values ¡ characters ¡ Followed ¡ ¡ Followed ¡ ¡ by ¡@ ¡ by ¡. ¡ 3 ¡

  4. Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡ Regular ¡expressions ¡(regex) ¡ • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡ • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡ Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ 4 ¡

  5. Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡ Regular ¡expressions ¡(regex) ¡ • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡ • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡ Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ R 1 ¡R 2 ¡ 5 ¡

  6. Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡ Regular ¡expressions ¡(regex) ¡ • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡ • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡ Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ R 1 ¡R 2 ¡ AlternaSve: ¡R 1 ¡| ¡R 2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ 6 ¡

  7. Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡ Regular ¡expressions ¡(regex) ¡ • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡ • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡ Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ R 1 ¡R 2 ¡ AlternaSve: ¡R 1 ¡| ¡R 2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡ reference/extracSon ¡ 7 ¡

  8. Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡ Regular ¡expressions ¡(regex) ¡ • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡ • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡ Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ R 1 ¡R 2 ¡ AlternaSve: ¡R 1 ¡| ¡R 2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡ reference/extracSon ¡ Character ¡classes ¡ AlternaSve ¡characters ¡and ¡ [a-­‑c] ¡matches ¡“a” ¡or ¡“b” ¡or ¡“c”, ¡ [c 1 -­‑c 2 ] ¡and ¡[^c 1 -­‑c 2 ] ¡ excluded ¡characters ¡ while ¡[^a-­‑c] ¡matches ¡any ¡but ¡abc ¡ 8 ¡

  9. Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡ Regular ¡expressions ¡(regex) ¡ • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡ • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡ Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ R 1 ¡R 2 ¡ AlternaSve: ¡R 1 ¡| ¡R 2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡ reference/extracSon ¡ Character ¡classes ¡ AlternaSve ¡characters ¡and ¡ [a-­‑c] ¡matches ¡“a” ¡or ¡“b” ¡or ¡“c”, ¡ [c 1 -­‑c 2 ] ¡and ¡[^c 1 -­‑c 2 ] ¡ excluded ¡characters ¡ while ¡[^a-­‑c] ¡matches ¡any ¡but ¡abc ¡ RepeSSon: ¡R* ¡ Matches ¡0 ¡or ¡more ¡Smes ¡ “ca*t” ¡matches ¡“ct”, ¡“cat”, ¡“caat” ¡ 9 ¡

  10. Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡ Regular ¡expressions ¡(regex) ¡ • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡ • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡ Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ R 1 ¡R 2 ¡ AlternaSve: ¡R 1 ¡| ¡R 2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡ reference/extracSon ¡ Character ¡classes ¡ AlternaSve ¡characters ¡and ¡ [a-­‑c] ¡matches ¡“a” ¡or ¡“b” ¡or ¡“c”, ¡ [c 1 -­‑c 2 ] ¡and ¡[^c 1 -­‑c 2 ] ¡ excluded ¡characters ¡ while ¡[^a-­‑c] ¡matches ¡any ¡but ¡abc ¡ RepeSSon: ¡R* ¡ Matches ¡0 ¡or ¡more ¡Smes ¡ “ca*t” ¡matches ¡“ct”, ¡“cat”, ¡“caat” ¡ Non-­‑zero ¡ Matches ¡1 ¡or ¡more ¡Smes ¡ “ca+t” ¡matches ¡“cat” ¡or ¡“caat” ¡or ¡ repeSSon: ¡R+ ¡ “caaat”, ¡but ¡not ¡“ct” ¡ 10 ¡

  11. We ¡can ¡use ¡RegEx ¡to ¡see ¡if ¡an ¡email ¡ address ¡is ¡valid ¡ ¡ Email ¡addresses ¡follow ¡a ¡paHern: ¡ ¡ mailbox@domain.TLD ¡ ¡ ¡ example: ¡tjp@cs.dartmouth.edu ¡ ¡ We ¡can ¡specify ¡a ¡paHern ¡or ¡rules ¡for ¡email ¡addresses: ¡ ¡ <characters> ¡@ ¡<characters>.<com ¡| ¡edu ¡| ¡org ¡| ¡…> ¡ ¡ As ¡a ¡simple ¡RegEx: ¡[a-­‑z]+@[a-­‑z.]* ¡[a-­‑z]+. ¡(com ¡| ¡edu ¡| ¡org ¡…) ¡ Check: ¡ tjp@cs.dartmouth.edu ¡-­‑-­‑ ¡valid ¡ Blob.x ¡-­‑-­‑ ¡invalid ¡

  12. A ¡Graph ¡can ¡implement ¡a ¡RegEx ¡ Email ¡addresses ¡follow ¡a ¡paHern: ¡ ¡ mailbox@domain.TLD ¡ ¡ ¡ example: ¡tjp@cs.dartmouth.edu ¡ com ¡ ¡ Start ¡ ¡ . ¡ a-­‑z ¡ @ ¡ a-­‑z ¡ ¡ edu ¡ ¡ ¡ ¡ org ¡ We ¡can ¡specify ¡a ¡paHern ¡or ¡rules ¡for ¡email ¡addresses: ¡ ¡ <characters> ¡@ ¡<characters>.<com ¡| ¡edu ¡| ¡org ¡| ¡…> ¡ . ¡ ¡ . ¡ A ¡Graph ¡can ¡represent ¡the ¡paHern ¡for ¡email ¡addresses ¡ . ¡ Sample ¡addresses ¡can ¡be ¡easily ¡verified ¡if ¡in ¡correct ¡form ¡ 12 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend