Basic Python | String Manipulation & Regular Expressions


One place where the Python language really shines is in the manipulation of strings. This section will cover some of Python's built-in string methods and formatting operations, before moving on to a quick guide to the extremely useful subject of regular expressions. Such string manipulation patterns come up often in the context of data science work, and is one big perk of Python in this context. Shown below are few string manipulations, self explanatory.
Regular expressions generalize this "wildcard" idea to a wide range of flexible string-matching syntax's. The Python interface to regular expressions is contained in the built-in re module. As a simple example, let's use it to duplicate the functionality of the string split() method.
The following table lists a few of these characters that are commonly useful and this is not a comprehensive list or description.
CharacterDescriptionCharacterDescription
"\d"Match any digit"\D"Match any non-digit
"\s"Match any whitespace"\S"Match any non-whitespace
"\w"Match any alphanumeric char"\W"Match any non-alphanumeric char
The following is a table of the repetition markers available for use in regular expressions:
CharacterDescriptionExample
?Match zero or one repetitions of preceding"ab?" matches "a" or "ab"
*Match zero or more repetitions of preceding"ab*" matches "a""ab""abb""abbb"...
+Match one or more repetitions of preceding"ab+" matches "ab""abb""abbb"... but not "a"
{n}Match n repetitions of preeeding"ab{2}" matches "abb"
{m,n}Match between m and n repetitions of preceding"ab{2,3}" matches "abb" or "abbb"
With these basics in mind, let's return to our email address pattern match,

No comments:

Post a Comment

Note: only a member of this blog may post a comment.