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

regular expressions in python
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

Regular Expressions in Python RE Module Basics of REs RE functions

Regular expression module

Module

In order to use regular expressions, we need to load the module. import re

2 / 9

slide-3
SLIDE 3

Regular Expressions in Python RE Module Basics of REs RE functions

Regular expression symbols

. wildcard

\

escapes specials characters

[. . .]

character set

[∧ . . .]

complement of character set

|

  • r

* 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

slide-4
SLIDE 4

Regular Expressions in Python RE Module Basics of REs RE functions

Understanding regular expressions

See slides 28–37 here: http: //cl.indiana.edu/∼md7/16/245/slides/04-searching/slides.pdf

4 / 9

slide-5
SLIDE 5

Regular Expressions in Python RE Module Basics of REs RE functions

Regular expression 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

slide-6
SLIDE 6

Regular Expressions in Python RE Module Basics of REs RE functions

Example

import re mysent = input ( ’ Give me a sentence !\ n ’ ) i f ( not re . search ( ’ [ ! \ . ; ? ] ’ , mysent ) ) : print ( ’ t h i s i s not a sentence ’ )

6 / 9

slide-7
SLIDE 7

Regular Expressions in Python RE Module Basics of REs RE functions

Example

import re mysent = input ( ’ Give me a sentence !\ n ’ ) newstr = re . sub ( ’ [A−Z ] ’ , ’XX ’ , mysent ) print ( newstr )

7 / 9

slide-8
SLIDE 8

Regular Expressions in Python RE Module Basics of REs RE functions

Pattern objects

Module

The functions compile, search, and match return a 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

slide-9
SLIDE 9

Regular Expressions in Python RE Module Basics of REs RE functions

Example

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