Detailed explanation of how Python uses regular expressions to exclude characters from a set

黄舟
Release: 2017-10-10 10:59:31
Original
1616 people have browsed it

In regular expressions, if you want to match one of some characters, that is to say, given a set of characters, as long as any character in this set appears, it is established. The following article mainly introduces it to you. For information about Python's function of using regular expressions to exclude characters from a set, friends in need can refer to it.

Preface

We have learned before that through the set enumeration function, we can list all the characters that need to appear and save them in the set. , so that the regular expression can determine whether the match is successful based on whether the character exists in the set. If it is in the set, the match is successful, otherwise it is unsuccessful. Now there is a problem, that is, the characters listed in the set must not appear before the match is successful. How to achieve this requirement? In fact, it is relatively simple. You only need to add a character ^ in front of the set, so that when the regular expression is matched, if a character is found in the set, the match will fail. Not much to say below, let’s take a look at the detailed introduction.

The example is as follows:


#python 3.6 
#蔡军生 
#http://blog.csdn.net/caimouse/article/details/51749579 
# 
from re_test_patterns import test_patterns 
 
test_patterns( 
 'This is some text -- with punctuation.', 
 [('[^-. ]+', 'sequences without -, ., or space')], 
)
Copy after login

The result output is as follows:


'[^-. ]+' (sequences without -, ., or space)

 'This is some text -- with punctuation.'
 'This'
 .....'is'
 ........'some'
 .............'text'
 .....................'with'
 ..........................'punctuation'
Copy after login

In this example, characters such as dashes, periods, and spaces (-, ., ,) are excluded.

Summarize

The above is the detailed content of Detailed explanation of how Python uses regular expressions to exclude characters from a set. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template