Use wildcards to match strings:
Use the
fnmatch.filter()
method to get the strings matching the pattern from the list.Use the
fnmatch.fnmatch()
method to check whether a string matches a pattern.
import fnmatch a_list = ['fql.txt', 'jiyik.txt', 'com.csv'] pattern = '*.txt' filtered_list = fnmatch.filter(a_list, pattern) print(filtered_list) # ????️ ['fql.txt', 'jiyik.txt']
If we prefer to use regular expressions, please scroll down to the next subtitle.
fnmatch.filter
method accepts an iterable object and a pattern and returns a new list containing only the iterable object elements that match the provided pattern.
The pattern in the example starts with any one or more characters and ends with .txt
.
The pattern in the example contains only one wildcard, but you can use as many wildcards as you want.
Note that the asterisk
*
matches everything (one or more characters).
If you want to match any single character, replace the asterisk *
with the question mark ?
.
*
Matches everything (one or more characters)?
Matches anything A single character[sequence]
matches any character in the sequence[!sequence]
Matches any non-sequential characters
Here is an example of using a question mark to match any single character.
import fnmatch a_list = ['abc', 'abz', 'abxyz'] pattern = 'ab?' filtered_list = fnmatch.filter(a_list, pattern) print(filtered_list) # ????️ ['abc', 'abz']
This pattern matches a string starting with ab followed by any single character.
If you want to use wildcards to check whether a string matches a pattern, use the fnmatch.fnmatch()
method.
import fnmatch a_string = '2023_jiyik.txt' pattern = '2023*.txt' matches_pattern = fnmatch.fnmatch(a_string, pattern) print(matches_pattern) # ????️ True if matches_pattern: # ????️ this runs print('The string matches the pattern') else: print('The string does NOT match the pattern')
The pattern starts with 2023, followed by any one or more characters, and ends with .txt.
fnmatch.fnmatch
The method accepts a string and a pattern as parameters. If the string matches the pattern, the method returns True, otherwise it returns False. Just replace the asterisk*
with the question mark?
if you want to match any single character instead of any one or more characters.
Alternatively, we can use regular expressions.
Use regular expressions to match strings using wildcards
Use wildcards to match strings:
Use re.match()
Method checks if a string matches the given pattern. Use .*
characters instead of wildcard characters. The
import re a_list = ['2023_fql.txt', '2023_jiyik.txt', '2023_com.csv'] regex = re.compile(r'2023_.*\.txt') list_of_matches = [ item for item in a_list if re.match(regex, item) ] print(list_of_matches) # ????️ ['2023_fql.txt', '2023_jiyik.txt']
re.compile
method compiles a regular expression pattern into an object that can be used using its match()
or search()
method to match.
This is more efficient than using re.match
or re.search
directly because it saves and reuses the regular expression object.
Regular expression starts with 2023_.
The
.*
characters in regular expressions are used as wildcards to match any one or more characters.
Dot
.
matches any character except a newline character.The asterisk
*
matches the preceding regular expression (dot.
) zero or more times.
We use the backslash\ character to escape dots. in the extension because, as we saw before, the dot
.
has a special meaning when used in regular expressions. In other words, we use backslashes to handle dots. as literal characters.
We use list comprehension to iterate over the list of strings.
List comprehensions are used to perform certain operations on each element or to select a subset of elements that meet a condition.
In each iteration, we use the re.match()
method to check if the current string matches the pattern.
import re a_list = ['2023_fql.txt', '2023_jiyik.txt', '2023_com.csv'] regex = re.compile(r'2023_.*\.txt') list_of_matches = [ item for item in a_list if re.match(regex, item) ] print(list_of_matches) # ????️ ['2023_fql.txt', '2023_jiyik.txt']
The re.match
method returns a match object if the provided regular expression matches in the string.
If the string does not match the regular expression pattern, the
match()
method returns None.
The new list contains only the strings in the original list that match the pattern.
If you only want to match any single character, remove the asterisk after the dot *.
in the regular expression.
import re a_list = ['2023_a.txt', '2023_bcde.txt', '2023_z.txt'] regex = re.compile(r'2023_.\.txt') list_of_matches = [ item for item in a_list if re.match(regex, item) ] print(list_of_matches) # ????️ ['2023_a.txt', '2023_z.txt']
Dot .
Matches any character except newlines.
If you need help reading or writing regular expressions, please refer to our regular expression tutorial. This page contains a list of all special characters and many useful examples. If you want to use regular expressions to check whether a string matches a pattern, we can directly use theBy using dots
.
without escaping, the regular expression matches anything starting with 2023_ followed by any single character ending with ## The string ending in #.txt.
re.match() method.
import re a_string = '2023_fql.txt' matches_pattern = bool(re.match(r'2023_.*\.txt', a_string)) print(matches_pattern) # ????️ True if matches_pattern: # ????️ this runs print('The string matches the pattern') else: print('The string does NOT match the pattern')
如果字符串与模式匹配,则
re.match()
方法将返回一个匹配对象,如果不匹配,则返回 None 。
我们使用 bool()
类将结果转换为布尔值。
如果要对单个字符使用通配符,请删除星号 *
。
import re a_string = '2023_ABC.txt' matches_pattern = bool(re.match(r'2023_.\.txt', a_string)) print(matches_pattern) # ????️ False if matches_pattern: print('The string matches the pattern') else: # ????️ this runs print('The string does NOT match the pattern')
请注意
,点.
我们没有使用反斜杠作为前缀用于匹配任何单个字符,而点.
我们以反斜杠 \ 为前缀的被视为文字点。
示例中的字符串与模式不匹配,因此 matches_pattern
变量存储一个 False 值。
The above is the detailed content of How to use wildcards to match strings in Python. For more information, please follow other related articles on the PHP Chinese website!

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.

Python is suitable for rapid development and data processing, while C is suitable for high performance and underlying control. 1) Python is easy to use, with concise syntax, and is suitable for data science and web development. 2) C has high performance and accurate control, and is often used in gaming and system programming.

The time required to learn Python varies from person to person, mainly influenced by previous programming experience, learning motivation, learning resources and methods, and learning rhythm. Set realistic learning goals and learn best through practical projects.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools