Mindmajix

Regular expression operations

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.

The 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:

  1. Search- index, find and count
  2. Replacing-replace
  3. Parsing-split

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:

Screenshot_19

See how easily we converted John from good boy to bad boy.
Now, here is another example:

Screenshot_20

Now have a look at this

Screenshot_21

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:

Screenshot_22

Now, let’s have a look at how we can actually use the ‘ re ’ module

Screenshot_23

$ helps in replacing characters if they match the characters at the end of the string

Screenshot_24

‘^’ 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:

Screenshot_25

You can also prefix letter ‘ r ‘ in front of the string, if you want to work with raw string.

Screenshot_26

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:

Screenshot_27

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

Screenshot_28

Now let’s try to understand the difference between match and search:

Screenshot_29

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.

https://docs.python.org/2/library/re.html

Enroll for Instructor Led Live Python Training


0 Responses on Regular expression operations"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.