The Python regular expression re module uses

Source: Internet
Author: User

Re is a regular expression module for Python, which records the use of the actual development process.

The best practice for regular expressions is to use the Re.compile method after compilation, which is more efficient


1, Re.search ()

Example

TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up, 23:51, 2 users, load average:0.02, 0.03, 0.10


Match TS begins with a space followed by several numbers.

>>> import re>>> str= "TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up + +, 23:51, 2 users, Load average:0.02, 0.03, 0.10 ">>> import re>>> prog = Re.compile (R ' ^ts \d+ ') >>> m = Prog.sea RCH (str) >>> m.group (0) ' TS 1423031065 '


2, Re.split ()

Splitting strings is a common requirement, usually using the split method, but specifying multiple delimiters at a time is not supported by the split method, only with RE

The Split method.

Example

TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up, 23:51, 2 users, load average:0.02, 0.03, 0.10

Split the above string with a space and.

>>> str= "TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up +, 23:51, 2 users, load average:0.02, 0.03, 0.10 ">>> Re.split (R ' \s+|\. ', str) [' TS ', ' 1423031065 ', ' 017865025 ', ' 2015-02-04 ', ' 14:24:25 ', ' 14:24:25 ' , ' Up ', ' the ' 23:51 ', ' Days, ', ' the ', ', ' 2 ', ' Users, ', ' load ', ' average: ', ' 0 ', ' 02, ', ' 0 ', ' 03, ', ' 0 ', ' 10 ']



This article is from the "Candle Shadow Red" blog, be sure to keep this source http://gccmx.blog.51cto.com/479381/1611613

The Python regular expression re module uses

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.