Regular expressions in Python

Source: Internet
Author: User

Atomic

An atom is the most basic constituent unit of a regular expression, with at least one atom in each regular expression. Common types of atoms are:
A ordinary character as an atom
b nonprinting characters as atoms
C Universal characters as atoms
D Atomic table.

ImportRe String="Taoyunjiaoyu"#ordinary characters as atomspat="Yum"rst=Re.search (pat,string)Print(RST)#non-printable characters as atoms#\ n line break \ t tabstring='"" TaoyunjiaoyuBaidu'pat="\ n"rst=Re.search (pat,string)Print(RST)#universal characters as atoms\w Letters, numbers, underscores \w except for letters, numbers, underscores \d decimal digits \d except for decimal digits \s white space characters \s in addition to white space characters string=" "taoyunji8 7362387aoyubaidu ' "pat=" \w\d\s\d\d "Rst=re.search (pat,string) print (rst) #原子表string = ' ' Taoyunji87362387aoyubaidu "pat=" tao[abd] "pat=" Tao[^abd] "Rst=re.search (pat,string) print (RST)
View Code

Metacharacters

The so-called meta-character, is the regular expression has some special meaning of the characters, such as repeating n times before the characters and so on.

.  Any one character except newline ^ start position $ end Position * 0\1\ multiple times? 01 Times + 1\ Multiple {n} exactly n times {n,} at least n times in,m} at least N, up to M times | pattern selector or () mode unit
View Code

Pattern Repair Symbol

The so-called pattern modifier, which can change the meaning of the regular expression by the pattern modifier without changing the regular expression, realizes some functions such as adjusting the matching result.

I ignore case when matching

string="Python"Pat="pyt"rst=  Re.search (Pat,string,re. I)print(RST)
View Code

M multi-line matching

L Localization Identification match
U Unicode
s let. Match include line break

Greedy Mode & Lazy mode

The core point of greedy mode is to match as many as possible, while the core of lazy mode is to match as few as possible.

 #   greedy mode with lazy mode  String= " povthonyhjskjsa  "  PAT1  = p.*y   " #   greedy mode  Pat2= " p.*?y  "  #   lazy mode  rst= =re.search (PAT2,STRING,RE.L)  print   print  (RST2) 
View Code

Regular-expression functions

Re.match () function: Match from the beginning, matching a

Re.search () function: matches from any position, matching a

Global matching function

Global match format: Re.compile (regular expression). FindAll (data)

Regular expressions in Python

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.