How to match at the end of string in python using Regular Expression?

A regular expression in Python is a set of characters that allows you to use a search pattern to find a string or a group of strings. RegEx is another term for regular expressions.

Regular expressions are worked within Python using the re package.

To match with the end of the string in python by using a regular expression, we use ^/w+$ regular expression.


  • $ implies the start with.

  • /w returns a match where the string contains any word characters (a z, A Z, 0 9, and underscore character).

  • + indicates one or more occurrences of a character.

Using method

In the following example code, we match the word skills which is present at the end of the string ‘tutorialspoint is a great platform to enhance your skills’.

We begin by importing the regular expression module.

import re

Then, we have used the search() function which is imported from the re module. This function in Python searches the string for a match and returns a match object if there is any match. The group() method is used to return the part of the string that is matched.


import re s = 'tutorialspoint is a great platform to enhance your skills' result ='\w+$', s) print(


The following output is obtained on executing the above program.


Using re.findall() method

The method findall(pattern, string) in python locates every occurrence of the pattern within the string. The dollar-sign ($) guarantees that you only match the word Python at the end of the string when you use the pattern "\w+$".


import re text = 'tutorialspoint is a great platform to enhance your skills' result = re.findall(r'\w+$', text) print(result)


Following is an output of the above code


Updated on: 08-Nov-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started