Home >Backend Development >Python Tutorial >python example code using regular expression concatenator

python example code using regular expression concatenator

黄舟
黄舟Original
2017-10-10 10:50:392003browse

In regular expressions, it is very inconvenient to match numbers or English letters. Therefore, regular expressions introduce the connector "-" to define the range of characters. The following article mainly introduces you to the relevant information on how to use the connector of regular expressions in python. Friends who need it can refer to it.

Preface

In the previous example, we learned to use characters in the set or characters not in the set. Write out each character, but sometimes you need to put all 26 lowercase letters into a set. Then according to the set method, you have to enter 26 times, one by one, which takes more time and is prone to errors. So is there any What's a better way? This exists, it uses the function of the regular expression connector: -, for example, to represent 26 lowercase characters, just use [a-z].

This article introduces to you in detail the relevant content about Python's use of regular expression connectors, and shares it for your reference and study. I won't say much 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.', 
 [('[a-z]+', 'sequences of lowercase letters'), 
  ('[A-Z]+', 'sequences of uppercase letters'), 
  ('[a-zA-Z]+', 'sequences of letters of either case'), 
  ('[A-Z][a-z]+', 'one uppercase followed by lowercase')], 
)

The result output is as follows:


'[a-z]+' (sequences of lowercase letters)


 'This is some text -- with punctuation.'
 .'his'
 .....'is'
 ........'some'
 .............'text'
 .....................'with'
 ..........................'punctuation'


'[A-Z]+' (sequences of uppercase letters)


 'This is some text -- with punctuation.'
 'T'


'[a-zA-Z]+' (sequences of letters of either case)


 'This is some text -- with punctuation.'
 'This'
 .....'is'
 ........'some'
 .............'text'
 .....................'with'
 ..........................'punctuation'


'[A-Z][a-z]+' (one uppercase followed by lowercase)


 'This is some text -- with punctuation.'
 'This'

Summary

The above is the detailed content of python example code using regular expression concatenator. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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