Learn Python in One Hour by Victor R. Volkman

Learn Python in One Hour by Victor R. Volkman

Author:Victor R. Volkman
Language: eng
Format: epub
Publisher: Loving Healing Press Inc.


So we’ve solved two out of three criteria with this simple two-character regular expression (“to”): it matches all permutations of mixed-case including “TO”, “to”, “To” and “tO”, and it works around punctuation such as “to)”. Unfortunately, it found twice as many copies of the token by looking inside tattoo and tool:

"To tattoo (or not to) a tool"

1 2 3 4

Compare this with a brute-force approach of using split(), which is the only tool we had up until now! Isolating all the “to” tokens while ignoring when they appear inside other words would be a really hard problem except for a special character sequence “\b” which does this:

\b Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character.

Here’s a short program which marches through all these

1: # Exercise #8: case-insensitive search in regular expression

2: import re

3: regex = r"\bto\b"

4: test_str = "To be (or not to) be a tool."

5: matches = re.finditer(regex, test_str, re.IGNORECASE)

6:

7: for matchNum, match in enumerate(matches):

8: matchNum = matchNum + 1

9: print ("Match %s was found at %d-%d: %s" % (

10: matchNum, match.start(), match.end(), match.group()))



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.