52
Regular Expressions March 31 2015 CSCI 0931 Intro. to Comp. for the HumaniBes and Social Sciences 1

Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

March  31  2015  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   1  

Page 2: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Project  Proposal  Reminders  

•  Don’t  forget  to  include  the  skeleton  code  – Define  the  funcBons  you  are  using  – Comments  telling  us  the  inputs  and  outputs  

•  Numbered  list  of  steps  – Describe  how  funcBons  compute  –  If  you’re  tesBng  a  hypotheses,  you  should  explain  how  the  funcBons  help  test  it.  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   2  

Page 3: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Today  

•  Regular  Expressions  •  Using  regular  expressions  in  Python  •  If  we  have  Bme,  a  new  data  structure:  Iterator  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   3  

Page 4: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Today  

•  Regular  Expressions  –  a  way  to  match  strings  •  Using  regular  expressions  in  Python  •  If  we  have  Bme,  a  new  data  structure:  Iterator  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   4  

Page 5: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Play  Time!  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   5  

Page 6: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Play  Time!  Go  to  regexpal.com  and  copy  this  text  into  the  lower  box:    

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   6  

Ahab was born in 1802. Starbuck, in '98 --- 1998, that is. And (as everyone knows), Ishmael was born in 2036 but died in 1879 (making him [1879-2036=]-57 years old at the time of his death, or 48, depending on how you count). And the white whale was immortal. Am I telling this story right? Anyhow, each of them had access to a time machine, a harpoon, and 20+ sheep for barter. Including the whale. As was common in his era, Starbuck had his own name tattooed on the back of his arm: "*$", it read, "Star-buck", a kind of pun. Our story begins in interstellar space, in the year 2000 BC...

Try  entering  different  things  (leZers,  numbers,  symbols,  brackets)  in  the  upper  box.    

Any  surprises?  

 

Page 7: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Play  Time!  Go  to  regexpal.com  and  copy  this  text  into  the  lower  box:    

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   7  

Ahab was born in 1802. Starbuck, in '98 --- 1998, that is. And (as everyone knows), Ishmael was born in 2036 but died in 1879 (making him [1879-2036=]-57 years old at the time of his death, or 48, depending on how you count). And the white whale was immortal. Am I telling this story right? Anyhow, each of them had access to a time machine, a harpoon, and 20+ sheep for barter. Including the whale. As was common in his era, Starbuck had his own name tattooed on the back of his arm: "*$", it read, "Star-buck", a kind of pun. Our story begins in interstellar space, in the year 2000 BC...

Do  Task  1  

Page 8: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   8  

The qu1ck brown fox jumped over the lazy d0g.

#   #  

Page 9: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   9  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

       

The qu1ck brown fox jumped over the lazy d0g.

Page 10: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   10  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u

       

The qu1ck brown fox jumped over the lazy d0g.

Page 11: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   11  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

       

The qu1ck brown fox jumped over the lazy d0g.

Page 12: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   12  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer  

       

The qu1ck brown fox jumped over the lazy d0g.

Page 13: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   13  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w

       

The qu1ck brown fox jumped over the lazy d0g.

Page 14: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   14  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

       

The qu1ck brown fox jumped over the lazy d0g.

Page 15: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   15  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace  

       

The qu1ck brown fox jumped over the lazy d0g.

Page 16: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   16  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w

       

The qu1ck brown fox jumped over the lazy d0g.

Page 17: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   17  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

       

The qu1ck brown fox jumped over the lazy d0g.

Page 18: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   18  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit          

The qu1ck brown fox jumped over the lazy d0g.

Page 19: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   19  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w

       

The qu1ck brown fox jumped over the lazy d0g.

Page 20: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   20  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

       

The qu1ck brown fox jumped over the lazy d0g.

Page 21: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   21  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

   

The qu1ck brown fox jumped over the lazy d0g.

Page 22: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   22  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s

   

The qu1ck brown fox jumped over the lazy d0g.

Page 23: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   23  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

   

   

The qu1ck brown fox jumped over the lazy d0g.

Page 24: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   24  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

The qu1ck brown fox jumped over the lazy d0g.

Page 25: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   25  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w

The qu1ck brown fox jumped over the lazy d0g.

Page 26: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   26  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w The qu1ck brown ...

The qu1ck brown fox jumped over the lazy d0g.

Page 27: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   27  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w The qu1ck brown ...

.   Match  any  character  

The qu1ck brown fox jumped over the lazy d0g.

Page 28: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   28  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w The qu1ck brown ...

.   Match  any  character   .

The qu1ck brown fox jumped over the lazy d0g.

Page 29: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   29  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w The qu1ck brown ...

.   Match  any  character   . T h e ‘ ‘ q u ...

The qu1ck brown fox jumped over the lazy d0g.

Page 30: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   30  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w The qu1ck brown ...

.   Match  any  character   \s.+\s

The qu1ck brown fox jumped over the lazy d0g.

Page 31: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   31  

Special  Syntax  

Meaning   R.E  Example  

Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w The qu1ck brown ...

.   Match  any  character   \s.+\s qu1ck brown fox ...

The qu1ck brown fox jumped over the lazy d0g.

Page 32: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Today  

•  Regular  Expressions  –  a  way  to  match  strings  •  Using  regular  expressions  in  Python  •  If  Bme,  new  data  structure:  Iterator  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   32  

Page 33: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   33  

Special  Syntax  

Meaning   R.E   Result  

[  ]   Match  anything  between  brackets  

[qjd]u qu ju

\w   Match  any  leZer   o\w ow ox ov

\s   Match  any  whitespace   e\s\w ‘e q’ ‘e l’

\d   Match  any  digit   \d\w 1c 0g

+   Match  one  or  more  of  the  previous  things.  

\s\w+\s The brown fox ...

*   Match  zero  or  more  of  the  previous  things.  

\w\d*\w The qu1ck brown ...

.   Match  any  character   \s.+\s qu1ck brown fox ...

The qu1ck brown fox jumped over the lazy d0g.

Page 34: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Regular  Expressions  

Pa>ern   Descrip?on  

^   Matches  beginning  of  line.  

$   Matches  end  of  line.  

.   Matches  any  single  character  except  newline.  Using  m  opBon  allows  it  to  match  newline  as  well.  

[...]   Matches  any  single  character  in  brackets.  

[^...]   Matches  any  single  character  not  in  brackets  

re*   Matches  0  or  more  occurrences  of  preceding  expression.  

re+   Matches  1  or  more  occurrence  of  preceding  expression.  

re?   Matches  0  or  1  occurrence  of  preceding  expression.  

re{  n}   Matches  exactly  n  number  of  occurrences  of  preceding  expression.  

re{  n,}   Matches  n  or  more  occurrences  of  preceding  expression.  

re{  n,  m}   Matches  at  least  n  and  at  most  m  occurrences  of  preceding  expression.  

a|  b   Matches  either  a  or  b.  

(re)   Groups  regular  expressions  and  remembers  matched  text.  

…   …  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   34  

Just  the  beginning…  see  the  ‘PythonRE’  link  for  lots  more:  

Page 35: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Today  

•  Regular  Expressions  –  a  way  to  match  strings  •  Using  regular  expressions  in  Python  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   35  

Page 36: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

If  Bme…  

•  Regular  Expressions  –  a  way  to  match  strings  •  Using  regular  expressions  in  Python  •  New  data  structure:  Iterator  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   36  

Page 37: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Today  

•  Regular  Expressions  –  a  way  to  match  strings  •  Using  regular  expressions  in  Python  •  New  data  structure:  Iterator  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   37  

Page 38: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   38  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’ Lists  

Page 39: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   39  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Page 40: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   40  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Dic?onaries  

Page 41: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   41  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Dic?onaries  keys  &  values  

‘Alice’ -> ‘401-111-1111’

‘Bob’ -> ‘401-222-2222’

‘Carol’ -> ‘401-333-3333’

Page 42: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   42  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Dic?onaries  keys  &  values  

‘Alice’ -> ‘401-111-1111’

‘Bob’ -> ‘401-222-2222’

‘Carol’ -> ‘401-333-3333’

Iterators  Match  Objects  

Page 43: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   43  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Dic?onaries  keys  &  values  

‘Alice’ -> ‘401-111-1111’

‘Bob’ -> ‘401-222-2222’

‘Carol’ -> ‘401-333-3333’

Iterators  Match  Objects  

Match  1  

•  Matched  String  •  Matched  String  Start  •  Matched  String  End  

Page 44: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   44  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Dic?onaries  keys  &  values  

‘Alice’ -> ‘401-111-1111’

‘Bob’ -> ‘401-222-2222’

‘Carol’ -> ‘401-333-3333’

Iterators  Match  Objects  

Match  1  

Match  2  

•  Matched  String  •  Matched  String  Start  •  Matched  String  End  

Page 45: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   45  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Dic?onaries  keys  &  values  

‘Alice’ -> ‘401-111-1111’

‘Bob’ -> ‘401-222-2222’

‘Carol’ -> ‘401-333-3333’

Iterators  Match  Objects  

Match  1  

Match  2  

Match  3  

•  Matched  String  •  Matched  String  Start  •  Matched  String  End  

Page 46: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Data  Structures  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   46  

‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘i’ ‘j’

0 1 2 3 4 5 6 7 8 9 Lists  

indices  content  

Dic?onaries  keys  &  values  

‘Alice’ -> ‘401-111-1111’

‘Bob’ -> ‘401-222-2222’

‘Carol’ -> ‘401-333-3333’

Iterators  Match  Objects  

Match  1  

Match  2  

Match  3  

Match  4  

•  Matched  String  •  Matched  String  Start  •  Matched  String  End  

Page 47: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Iterators  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   47  

The cat in the hat sat on a mat

Regular Expression:‘\wat’

Page 48: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Iterators  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   48  

Iterator  Match  

1  

The cat in the hat sat on a mat

Regular Expression:‘\wat’

Page 49: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Iterators  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   49  

Iterator  Match  

1  Match  

2  

The cat in the hat sat on a mat

Regular Expression:‘\wat’

Page 50: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Iterators  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   50  

Iterator  Match  

1  Match  

2  Match  

3  

The cat in the hat sat on a mat

Regular Expression:‘\wat’

Page 51: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Iterators  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   51  

Iterator  Match  

1  Match  

2  Match  

3  Match  

4  

The cat in the hat sat on a mat

Regular Expression:‘\wat’

Page 52: Regular(Expressions(cs.brown.edu/courses/csci0931/2015-spring/3-synthesis/LEC3-1.pdf · Regular(Expressions(March(31(2015(CSCI0931(

Using  Regular  Expressions  in  Python  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  HumaniBes  and  Social  Sciences   52  

def printRegEx(regex,myStr): '''Prints all occurrences of the regular expression.''‘ myIter = re.finditer(regex,myStr) # Iterator! # This iterator contains MATCHES of the regex. # The following functions work on regex matches: # group(0): returns the string that matches the regex # start(0): the starting position of the string in myStr # end(0): the ending position of the string in myStr # For loops work on iterators in addition to lists # Each match of the regex in myStr is stored in # a variable called ‘occ' for occ in myIter: print 'matches',occ.group(0), \

'at positions',occ.start(0),'-',occ.end(0) return