Python Regular Expressions

Source: Internet
Author: User

First, the common method

FindAll: Matches all conforming content and returns a list containing the results

Search: Matches and extracts the first conforming content, returning a regular Expression object (object)

Sub: Replaces the content that matches the rule, returning the replaced value.

S: Indicates multi-line matching

Note: The difference between findall and search:

FindAll will iterate through the contents of the entire loading range, and return each match to the content;

Search once matched to the specified content will no longer match, return the results, searching the end.

Second, commonly used symbols:

. Number: Matches any character, except for line break \ n

>>>ImportRe>>> a='xy123'>>> B=re.findall ('x.', a)>>>Print(b) ['XY']>>> B=re.findall ('x..', a)>>>Print(b) ['xy1']

* Number: Match the previous character 0 or unlimited times

>>> a='xyxy123'>>> b=re.findall ('x*'  , a)print  (b) ['x''x ' "' "' "' "' "]

? Number: Match the previous character 0 or 1 times

>>> a='xyxy123'>>> b=re.findall ('x.? ' , a) Print b['x ' ' x ' "' "' "' "]

Note: ' Indicates a match 0 times

+ No: Matches the previous character at least once (one or more times)

>>> a='xyxy123'>>> b=re.findall ('x+'  , a)print  b['x'x']

. * Number: Greedy algorithm

Import re>>> secret_code='hadkfalifexxixxfasdjifja134xxlovexx23345sdfxxyouxx8dfse '>>> b=re.findall ('xx.*xx', secret_code)print b['xxixxfasdjifja134xxlovexx23345sdfxxyouxx']

. * No.: Non-greedy algorithm

Import re>>>secret_code='hadkfalifexxixxfasdjifja134xxlovexx23345sdfxxyouxx8dfse '>>> b=re.findall ('xx.*?xx', secret_code)print b['xxixx'xxlovexx ' Xxyouxx ']

(): The data in parentheses is returned as a result

Import re>>>secret_code='hadkfalifexxixxfasdjifja134xxlovexx23345sdfxxyouxx8dfse '>>> b=re.findall ('xx (. *?) xx', secret_code)print  b['I'  Love "You")

#匹配纯数据方法举例 (\d+)

>>> a='asdfefrg137143fdfhxs4321ddfdvsdf543dd'>>> b=re.findall ('  (\d+)', a)print  b['137143'  '4321'543']

Actual combat-making a text crawler

destination URL:http://www.imtech.res.in/raghava/sarpred/data/Manesh-215/PSSM/

Target content: Extract the specified file contents

Implementation principle:

1. Save the Web page source code

2.python read file load source code

3. Regular expression Extraction

4. Write # Coding=utf-8

ImportUrllibImportRedefgethtml (URL): page=urllib.urlopen (URL) HTML=Page.read ()returnHTMLdefgetreq (HTML):
REG1=r'href= "(. *?\.mtx)" >'REG2=r'href= "(. *?) \.MTX ">'MTX=re.compile (REG1) title=re.compile (REG2) mtxlist=Re.findall (mtx,html) titlelist=Re.findall (title,html) FP=open ('D://manesh-215.txt','W') I=0 forRequrlinchMtxlist:strurl='http://www.imtech.res.in/raghava/sarpred/data/Manesh-215/PSSM/'+Requrl mtxpage=Urllib.urlopen (strurl) mtxhtml=Mtxpage.readlines () St=">query |"+titlelist[i]+" | Length="+Mtxhtml[0] Fp.write (ST) Fp.write (mtxhtml[1]) I=i+1fp.close ()

Python Regular Expressions

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.