Example code for Python using regular expression connectors

Source: Internet
Author: User
In regular expressions, it is very inconvenient to have a matching number or a letter written in English. Therefore, the regular expression introduces the connector "-" to define the range of characters, the following article mainly introduces you about how to use the regular expression in Python related data, the need for friends can refer to.

Objective

We in the previous example, we learn to use the set of characters or non-set of characters, this is to write each character, but sometimes need to put 26 lowercase letters in the collection, then by the method of the collection, you have to enter 26 times, a type to go, so more time, also prone to error, So is there a better way? This is the use of the regular expression of the connector function:-, for example, to represent 26 lowercase characters, you can use [A-z] on it.

This article gives you a detailed introduction of Python using regular expression of the connector of the relevant content, share it for everyone to reference the study, the following words do not say, come together to see the detailed introduction bar.

Examples are as follows:


#python 3.6 #蔡军生 #http://blog.csdn.net/caimouse/article/details/51749579 # from Re_test_patterns import test_patterns< C0/>test_patterns (  ' This was some text--with punctuation. ',  [(' [a-z]+ ', ' sequences of lowercase '),   (' [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 resulting output is as follows:


' [a-z]+ ' (sequences of lowercase letters) ' This was some text--with punctuation '. ' His ' .... ' is ' ..... ' Some ' ..... ... ' with ' ..... ' ... '.. ... ' ..... ' ... '. ... ' ..... ' ... '. ... ' ... ' with '. ... '. ... '.... ' Punctuation ' [a-z]+ ' (sequences of uppercase letters) ' This was some text--with punctuation. ' ' T ' [a-za-z]+ ' (sequences of letters of either case) ' This is the some text--with punctuation. ' ' This ' ..... ' is ' .... ' Some ' ..... ... ' with ' ..... ' ... '.. ... ' ..... ' ... '. ... ' ..... ' ... '. ... ' ... ' with '. ... '. ... '.... ' Punctuation ' [a-z][a-z]+ ' (one uppercase followed by lowercase) ' This is the some text--with punctuation. ' ' This '

Summarize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.