Regular Expression Python

Source: Internet
Author: User

Import re# Re.match () can match the string that begins with XXX = Re.match (r "H", "Hello Python") # Print (Ret.group ()) # Group RET = Re.match (r "([^-]*) -(\d+) "," 010-12345678 ") # Print (Ret.group (1)) # By referencing the data that is matched in the grouping, but be aware of the meta string, which is similar to R" "in this format ret = Re.match (R" < ([a-za-z]* >\w*</\1> "," 

  

Compile it once and not compile it later

Import restr = ' https://www.freebuf.com/page/357 ' pattern = Re.compile (R ' (p|s) \:\/\/www\. +?\.. +?) \/+? ', re. Dotall) match = Pattern.findall (str) print (match) print (match)

  

Ignore case

Add (? i) to the front of the regular expression

Import restr1 = "" "201.158.69.116--[03/jan/2013:21:17:20-0600] fwf[-] tip[-] 127.0.0.1:9000 0.007 0.007 MX PYTHONTAB.C Om get/html/test.html http/1.1 "$" 2426 "http://a.com" "es-es,es;q=0.8" "mozilla/5.0 (Windows NT 6.1) applewebkit/537.1 1 (khtml, like Gecko) chrome/23.0.1271.97 safari/537.11 "172.16.119.8-admin [15/aug/2011:18:17:50 +0800]" propfind/svn/ EAGLE http/1.1 "201 649172.16.119.8-admin [15/aug/2011:18:17:50 +0800]" Propfind/svn/eagle/!svn/vcc/default HTtP/1.1 "  207 401172.16.119.8-admin [15/aug/2011:18:17:50 +0800] "Propfind/svn/eagle/!svn/bln/31 http/1.1" 207 454172.16.119.8- admin [15/aug/2011:18:17:50 +0800] "Propfind/svn/eagle http/1.1" 207 649172.16.119.8-admin [15/aug/2011:18:17:50 +0800 ] "Propfind/svn/eagle/!svn/vcc/default http/1.1" 207 454 "" "pattern = Re.compile (R ' (? i) http/.+?\b[^\d]+? ( [2345]\d{2}) ', Re. Dotall) ret = Pattern.findall (str1) print (ret)

  

The dot number does not match the line break by default, and you need to set the RE to match the line break. Dotall

Pattern = Re.compile (R ' (? i) http/.+?\b[^\d]+? ( [2345]\d{2}) ', Re. Dotall)

  

Regular Expression Python

Related Article

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.