What is a regular expression in Python?

PythonServer Side ProgrammingProgramming

In simple words, a regular expression is a sequence of character(s) mainly used to find and replace patterns in a string or file. They are supported by most of the programming languages like python, perl, R, Java and so on.

Regular expressions are very useful in extracting information from text such as code, log files, spreadsheets, or even documents. We deal more with the practical uses of regular expressions.

The first thing to know when using regular expressions is that everything is basically a character, and we write patterns to match a specific sequence of characters (also known as a string). Most patterns use normal ASCII, which includes letters, digits, punctuation and other symbols on computer keyboards like %#$@!, but unicode characters can also be used to match any type of international text.

In Python, there is a module “re” that works on regular expressions. So you need to import library re before you can use regular expressions in Python.

The most common uses of regular expressions are:

Search a string (search and match)

Finding a string (findall)

Break string into a sub strings (split)

Replace part of a string (sub)

Published on 28-Dec-2017 13:04:59