0
537views
Explain regular expression methods match and search with its parameters.
1 Answer
1
9views

Solution:

A Regular Expression (RegEx) is a special sequence of characters that uses a search pattern to find a string or set of strings.

It can detect the presence or absence of a text by matching it with a particular pattern, and also can split a pattern into one or more sub-patterns.

while regular expressions can be defined with simple strings in the RE language, it is much more efficient, for matches that will be used over and over, to compile the expression into a Python Regular expression instance; The regular expression class defines match, search, find, and other useful methods.

For example:

enter image description here

Match ():

re. match () function of re in Python will search the regular expression pattern and return the first occurrence.

The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object. But if a match is found in some other line, the Python RegEx Match function returns null.

enter image description here

Output:

enter image description here

Search ():

To use the search () function, you need to import the Python re-module first and then execute the code. The Python re. search () function takes the “pattern” and “text” to scan from our main string.

enter image description here

Output:

Ques10

Please log in to add an answer.