How to solve problems with Python Regular Expression objects

Source: Internet
Author: User

Python Regular Expression objects have many problems in our use and need to be noticed. Next, let's take a look at how to apply Python Regular Expression objects to related technologies. Hope to help you.

Generation Method: Pass

 
 
  1. Re. compile (pattern, [flags]) Back
  2. Match (string [, pos [, endpos]); returns string [pos, endpos] matching
  3. MatchObject of pattern

Python code

 
 
  1. split( string[, maxsplit = 0])   
  2. findall( string[, pos[, endpos]])   
  3. sub( repl, string[, count = 0])  

These functions are the same as those in the re module, but the calling form is slightly different.

Re. Several Functions and several functions of the regular expression object have the same function, but if the same program
Using these functions multiple times, several functions of the regular expression object are more efficient.

Matchobject

Use re. match (......) And re. compile (......). Match return

This object has the following methods and attributes:

Method:

 
 
  1. group( [group1, ...])   
  2. groups( [default])   
  3. groupdict( [default])   
  4. start( [group])   
  5. end( [group])  

The best way to illustrate these functions is to give an example.

 
 
  1. matchObj = re.compile(r”(?P\d+)\.(\d*)”)   
  2. m = matchObj.match(’3.14sss’)   
  3. #m = re.match(r”(?P\d+)\.(\d*)”, ‘3.14sss’)   
  4. print m.group()   
  5. print m.group(0)   
  6. print m.group(1)   
  7. print m.group(2)   
  8. print m.group(1,2)   
  9. print m.group(0,1,2)   
  10. print m.groups()   
  11. print m.groupdict()   
  12. print m.start(2)   
  13. print m.string  

The output is as follows:

 
 
  1. 3.14   
  2. 3.14   
  3. 3   
  4. 14   
  5. (’3′, ‘14′)   
  6. (’3.14′, ‘3′, ‘14′)   
  7. (’3′, ‘14′)   
  8. {’int’: ‘3′}   
  9. 2   
  10. 3.14sss  

Therefore, group () and group (0) are returned, which match the string of the entire expression. In addition, group (I) is the regular expression that uses the '3. 14', '3', '14') can best illustrate the problem.

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.