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. These are used within Python using the re package.

To match at the end of string in Python using regular expressions, we use the $ metacharacter which anchors the pattern to the end of the string.

Understanding the Pattern

The common pattern to match word characters at the end of a string is:

\w+$

Here:

  • \w matches any word character (a-z, A-Z, 0-9, and underscore)

  • + indicates one or more occurrences of the preceding character

  • $ anchors the match to the end of the string

Using re.search() Method

The re.search() function searches the entire string for a match and returns a match object if found. The group() method returns the matched part of the string.

Example

Here is an example showing the usage of the re.search() method ?

import re

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

The output of the above code is ?

skills

Using re.findall() Method

The re.findall() method finds all occurrences of the pattern within the string and returns them as a list. With the pattern \w+$, it will return the last word of the string.

Example

Here is an example showing the usage of the re.findall() method ?

import re

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

The output of the above code is ?

['skills']

Using re.match() Method

The re.match() method checks for a match only at the beginning of the string. To capture the word at the end, we need to match the entire string and capture the last word using groups.

Example

Here is an example demonstrating the usage of the re.match() method ?

import re

text = 'tutorialspoint is a great platform to enhance your skills'
result = re.match(r'.*\b(\w+)$', text)
if result:
    print(result.group(1))

The output of the above code is ?

skills

Matching Specific Patterns at End

You can also match specific patterns at the end of strings ?

import re

# Match if string ends with 'skills'
text = 'tutorialspoint is a great platform to enhance your skills'
result = re.search(r'skills$', text)
if result:
    print("String ends with 'skills'")
else:
    print("String does not end with 'skills'")

# Match email domains at end
email = 'user@example.com'
domain = re.search(r'@(\w+\.\w+)$', email)
if domain:
    print("Domain:", domain.group(1))

The output of the above code is ?

String ends with 'skills'
Domain: example.com

Conclusion

Use the $ metacharacter to anchor patterns to the end of strings. The \w+$ pattern is commonly used to match the last word, while re.search() is typically the most straightforward method for end-of-string matching.

Updated on: 2026-03-24T18:54:19+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements