re Module in Python

re Module in Python

>>> dir(re)
[‘A’, ‘ASCII’, ‘DEBUG’, ‘DOTALL’, ‘I’, ‘IGNORECASE’, ‘L’, ‘LOCALE’, ‘M’, ‘MULTIL
INE’, ‘S’, ‘Scanner’, ‘T’, ‘TEMPLATE’, ‘U’, ‘UNICODE’, ‘VERBOSE’, ‘X’, ‘__all__’
, ‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__initializing__’, ‘__lo
ader__’, ‘__name__’, ‘__package__’, ‘__version__’, ‘_alphanum_bytes’, ‘_alphanum
_str’, ‘_compile’, ‘_compile_repl’, ‘_expand’, ‘_pattern_type’, ‘_pickle’, ‘_sub
x’, ‘compile’, ‘copyreg’, ‘error’, ‘escape’, ‘findall’, ‘finditer’, ‘functools’,
 ‘match’, ‘purge’, ‘search’, ‘split’, ‘sre_compile’, ‘sre_parse’, ‘sub’, ‘subn’,
 ‘sys’, ‘template’]
>>>

Regular expressions use the following characters:

., ^, $, *, +, ?, {, }, [, ], \, |, (, )

findall

>>> z = “We love learning Polish because it is a very interesting language”
>>> z
‘We love learning Polish because it is a very interesting language’
>>> re.findall(“i”, z)
[‘i’, ‘i’, ‘i’, ‘i’, ‘i’, ‘i’]
>>> re.findall(“i.”, z)
[‘in’, ‘is’, ‘it’, ‘is’, ‘in’, ‘in’]
>>> re.findall(“^W”, z)
[‘W’]
>>> re.findall(“^P”, z)
[]
>>> re.findall(“guage$”, z)
[‘guage’]
>>> re.findall(“because$”, z)
[]
>>> re.findall(“i[a-z]?”, z)
[‘in’, ‘is’, ‘it’, ‘is’, ‘in’, ‘in’]
>>> re.findall(“i[a-z]??”, z)
[‘i’, ‘i’, ‘i’, ‘i’, ‘i’, ‘i’]
>>>

split

>>> re.split(” “, z)
[‘We’, ‘love’, ‘learning’, ‘Polish’, ‘because’, ‘it’, ‘is’, ‘a’, ‘very’, ‘intere
sting’, ‘language’]
>>> re.split(” “, z)

search

>>>
>>> b = re.search(“Polish”, z)
>>> b
<_sre.SRE_Match object at 0x00C1DA30>

>>> print(b.start(), b.end(), b.group(0))
17 23 Polish
>>>

>>> c = re.search(“in”, z)
>>> print(c.start(), c.end(), c.group(0))
13 15 in

We can search in our string with the pattern we have. Then, we can print it using the start of the string, the end of the string, and the value of the pattern. When there’s 0, we will get the first value, when there’s 1, we will get the second, and so on.

sub

>>> q = re.sub(“Polish”, “Russian”, z)
>>> print(q)
We love learning Russian because it is a very interesting language
>>>

We can change something in our string, replacing words.

match

>>> f = re.match(“^H”, “Hello world”)
>>> f.group(0)
‘H’
>>>

We can match anything in our string.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s