regular expressions in python
play

Regular Expressions in Python L435/L555 Dept. of Linguistics, - PowerPoint PPT Presentation

Regular Expressions in Python RE Module Basics of REs RE functions Regular Expressions in Python L435/L555 Dept. of Linguistics, Indiana University Fall 2016 1 / 9 Regular expression module Regular Expressions in Python RE Module


  1. Regular Expressions in Python RE Module Basics of REs RE functions Regular Expressions in Python L435/L555 Dept. of Linguistics, Indiana University Fall 2016 1 / 9

  2. Regular expression module Regular Expressions in Python RE Module Basics of REs RE functions Module In order to use regular expressions, we need to load the module. import re 2 / 9

  3. Regular expression symbols Regular Expressions in Python RE Module Basics of REs . wildcard RE functions \ escapes specials characters [ . . . ] character set [ ∧ . . . ] complement of character set | or * Kleene star: 0 or more (of previous) + Kleene plus: 1 or more (of previous) { m,n } repeat between m and n times ∧ beginning of a string $ end of a string 3 / 9

  4. Understanding regular expressions Regular Expressions in Python RE Module Basics of REs RE functions See slides 28–37 here: http: //cl.indiana.edu/ ∼ md7/16/245/slides/04-searching/slides.pdf 4 / 9

  5. Regular expression functions Regular Expressions in Python RE Module Basics of REs RE functions compile( < pattern > ) compiles a regex pattern into a pattern object – for reuse search( < pattern > , < string > ) searches for regex pattern in string match( < pattern > , < string > ) checks at beginning of string split( < pattern > , < string > ) splits the string based on pat- tern, returns a list findall( < pattern > , < string > ) returns a list of all occurrences sub( < pat > , < rep > , < string > ) replaces pat by rep in string 5 / 9

  6. Example Regular Expressions in Python RE Module Basics of REs RE functions import re mysent = input ( ’ Give me a sentence ! \ n ’ ) ( not re . search ( ’ [ ! \ . ; ? ] ’ , mysent ) ) : i f print ( ’ t h i s i s not a sentence ’ ) 6 / 9

  7. Example Regular Expressions in Python RE Module Basics of REs RE functions import re mysent = input ( ’ Give me a sentence ! \ n ’ ) newstr = re . sub ( ’ [A − Z ] ’ , ’XX ’ , mysent ) print ( newstr ) 7 / 9

  8. Pattern objects Regular Expressions in Python Module RE Module Basics of REs The functions compile, search, and match return a RE functions pattern object. The objects contain information about the pattern itself and for the matching functions also information about the matched segments in the string. import re phoneNums = re . compile ( ’ ˆ \ (? \ d { 3 } [ − ) ] \ d { 3 } [ − ] \ d { 4 } $ ’ ) myphone = input ( ’ Give me a phone number : ’ ) i f phoneNums . search (myphone ) : print ( ’ format correct ’ ) else : print ( ’ format i n c o r r e c t ’ ) 8 / 9

  9. Example Regular Expressions in Python RE Module Basics of REs RE functions import re mysent = ’ a rose i s a rose i s a rose ’ a l l s t r = re . search ( ’ ( . ) ’ , mysent ) print ( a l l s t r . group ( 1 ) ) a l l s t r = re . f i n d a l l ( ’ ( . ) ’ , mysent ) print ( a l l s t r ) 9 / 9

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