Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Regular expressions in python
  5. Search Function

Search Function

Regular expression in is a sequence of characters that forms a search pattern.

It is used to confirm whether the string has that particular term or not.

We can easily import regular expression in python.

  • [] – A set of characters
  • \ – Signals a special sequence.
  • . – Any character
  • ^ – Starts with
  • $ – Ends with
  • * – Zero or more occurrence
  • + – One or more occurrence
  • {} – Exactly the specified number of occurrences.
  • | – Either or
  • () – Capture and group

Special sequence:-

  • \d Matches any decimal digit, this is equivalent to the set class [0-9].
  • \D Matches any non-digit character.
  • \s Matches any whitespace character.
  • \S Matches any non-whitespace character
  • \w Matches any alphanumeric character, this is equivalent to the class [a-zA-Z0-9_].
  • \W Matches any non-alphanumeric character.

Search function searches the whole string to match the substring and returns its index.

syntax:-

re.search(substring, string, flag)

e.g.,

import re
a="Python"
s=''' One of the most famous programming language in the world is Python.'''
print(re.search(a, s))

This is all about search function in python.

Was this article helpful to you? Yes 1 No

How can we help?