[Tutor] Query regarding Regular Expression

cookiestar227 - Cookie Productions kay.budo at gmail.com
Wed Jun 28 16:27:53 EDT 2017


I am using Python version 3.6.0 and am learning Python as my very first
programming language.

Right now I am studying Regular Expressions.  We are finding all of the
matched substrings in a string using the "findall" method with the
following syntax:

re.findall(pattern, string[, flags])

Here is a link to the page tutorial I am using:

http://www.python-course.eu/python3_re_advanced.php

 So far have understood everything except for the following example:

>>>  t = "A fat cat doesn't eat oat but a rat eats bats."
>>>  mo = re.findall("[force]at", t)
>>>  print(mo)
['fat', 'cat', 'eat', 'oat', 'rat', 'eat']

What I don't understand is the [force] part of the Regular Expression.  I
cannot find anywhere online where there is an explanation of what [force]
means or what is does.  Why does it match to 'eat' from 'eats'? But not
'bat' from 'bats'?

I would prefer to use the following RE as it achieves my desired result:

>>> mo = re.findall("[A-Za-z]at", t)
>>> print(mo)
['fat', 'cat', 'eat', 'oat', 'rat', 'eat',  'bat']

Can anyone tell me what the [force] means and why it was used?

Thanks!

Kelly


More information about the Tutor mailing list