Regular expression operations
Recommended by 0 users
The Regular expression in Python is required when there is a need to match or look for strings. It provides a special sequence of characters to do the job.
The regular expressions can be used with the help of ‘re’ module. Whenever an error occurs, while compiling a regular expression the ‘re’ module raises re.error exception.
re module was added in Python 1.5, and provides Perl-style regular expression patterns. Earlier versions of Python came with the
regex module, which provided Emacs-style patterns. The
regex module was removed completely in Python 2.5.
Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the
re module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like. You can also use REs to modify a string or to split it apart in various ways.
The regular expression language is relatively small and restricted, so not all possible string processing tasks can be done using regular expressions. There are also tasks that can be done with regular expressions, but the expressions turn out to be very complicated. In these cases, you may be better off writing Python code to do the processing; while Python code will be slower than an elaborate regular expression, it will also probably be more understandable.
The following methods are provided for a string:
- Search- index, find and count
Most search operations for strings requires looking for case-sensitive characters. When you have to conduct a case-insensitive search you must call string_name.lower() or string_name.upper() methods and ensure that the search strings have the right case to match.
So, let’s get started with examples that will give you a better idea about how things work.
Example to replace a string:
See how easily we converted John from good boy to bad boy.
Now, here is another example:
Now have a look at this
It is a very interesting example which says only to search and replace ‘way’ in the last three characters of the string and leave the rest of the string(s[:-3]). The following examples will help you understand the concept in more detail:
Now, let’s have a look at how we can actually use the ‘ re ’ module
$ helps in replacing characters if they match the characters at the end of the string
‘^’ helps in replacing characters if they match the characters at the beginning of the string.
In the first example, the sub function helps in identifying the ‘house’ at the end of string_value and replaces it and it also ignores house at the beginning of the string. Similarly, in the second example, ‘house’ in the beginning of the string is replaced and its occurrence at the end of the string is ignored.
When you want to match a whole word and not a part of the word at the end of a string you can use \b as shown below:
You can also prefix letter ‘ r ‘ in front of the string, if you want to work with raw string.
When you want to look for a whole word (and not at a part of a big word) in the entire string, then replace it. Then it is achieved as follows:
Now let’s take a look at re.match function which will match the object and return the result. Here, we make use of group(number) and group() function. While the latter returns all the matching subgroups, the former looks for a match at a specific subgroup number
Now let’s try to understand the difference between match and search:
So, you can see that ‘ match ’ looks for a match at the beginning of the string, whereas ‘ search ’ checks the entire string for a match.
Enroll for Instructor Led Live Python Training