Syntax error How to write Python regular expression to match with file extension?

How to write Python regular expression to match with file extension?



Using Python, we can easily search for specific types of files from a mixed list of file names using regular expressions. For this, we need to import Python's built-in module called re using the import keyword.

The Regular expression or Regex is a special sequence of characters like \, *, ^, etc, which are used to search for a pattern in a string or a set of strings. It can detect the presence or absence of characters by matching them with a particular pattern and can also split a string into one or more substrings.

The following is a syntax to import the Regex module -

import re

Usage of 're.search()' function in Regex module

The re.search() is used to search for the first occurrence of a character or pattern in a string. If the search character is not found in the string, it will return None. If they give characters present in it will return -

  • Match Object: This indicates that the search() function has found a match and returned a match object. The match object stores details about the match.
  • span(): This will return the start index and end index values of the matched characters.
  • match: This is the actual substring that was matched in the search.

Following is the syntax of the re.search() function from the regex module -

re.search(pattern, string, flags=0)

The following are the parameters of the re.search() function from the regex module -

  • pattern: This is the regular expression to be matched
  • string: This is the string that would be searched to match the pattern anywhere in the string.
  • Flags: It is an optional parameter; different flags can be specified using bitwise OR (|).

Following is a basic example of the re.search() function from the regex module -

import re
s = "Welcome to Tutorialspoint"
res = re.search(r"o", s)
print(res)

Following is the output of the above code -

<re.Match object; span=(4, 5), match='o'>

Regular Expression to Match with File Extension

In the following example, we have a list of file names with various extensions. Using the regex module, we check if any file in the list ends with the .txt extension. If a match is found, the corresponding file name is printed -

The dollar symbol ($) is a metacharacter used in regular expressions to match the end of a string. It checks whether the string ends with the specified characters.

# import library 
import re 

# list of different types of file 
filenames = ["tp.html", "tutorial.xml", "tutorialspoint.txt", "tutorials_point.jpg"] 

for file in filenames: 
   # search given pattern in the line 
   match = re.search("\.txt$", file) 
   # if match is found 
   if match: 
      print("The file ending with .txt is -", file) 

Output

Following is the output of the above code -

The file ending with .txt is - tutorialspoint.txt
Updated on: 2025-04-15T12:34:02+05:30

917 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements