Python base 8.3 Match method and search method

Source: Internet
Author: User

One, the split method of the regular objectsplit (String[,maxsplit])returns the list after a string is chuanjiang to match the word. The maxsplit is used to specify the maximum number of splits and does not specify that all will be split. To find the word character that matches the object. #/usr/bin/python#coding =utf-8# @Time: 2017/11/18 20:52# @Auther: Liuzhenchuan# @File: Re's matche and seach.pyImport re Common methods of print ' regular 'a = Re.compile (R ' \d ') print dir (a) print ' # #'* + '\ n' >>>common methods of regular[' __class__ ', ' __copy__ ', ' __deepcopy__ ', ' __delattr__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' findall ', ' finditer ', ' flags ', ' groupindex ', ' groups ', ' match ', ' pattern ', ' scanner ', ' Search ', ' split ', ' Sub ', ' subn ']#1 Match Method--matches. Match (String[,pos[,endpos])#string: Matching the text used#pos: The index of the text in which the expression starts searching. and start searching for the subscript for string#endpos: The index of the end-of-search expression in text#如果不指定破损, default matches from the beginning, if not matched, returns none directlyprint ' # #match matching apps ' a = ' Hello world Hello Liu ' #正则匹配除 Hello World with Hello Liureg = Re.compile (R ' ((Hello w.*) (hello l.* ))result = Reg.match (a)print result.groups () print ' # #'*+ '\ n' >>># #match Matching apps(' Hello World Hello Liu ', ' Hello World ', ' Hello Liu ')############################################################print ' # #match added start position and end position, match (str,2,) Start position 2, to end ' b = ' AA ' + a print ' string B is: '+ b result2 = Reg.match (b,2,) reg = Re.compile (R ' ((Hello w.*) (hello l.* ))print result2.groups () print ' # #' * >>># #match added start position and end position, match (str,2,) Start position 2, to endstring B is: Aahello World Hello Liu(' Hello World Hello Liu ', ' Hello World ', ' Hello Liu ')############################## print ' #match写上起始位置与结束位置可以根据字符串b匹配除正则. Or you can match it with a \w .reg = Re.compile (R ' \w* ((hello w.*) (hello l.* ))RESULT3 = Reg.match (b)print result3.groups () print ' # #' * >>>#match写上起始位置与结束位置可以根据字符串b匹配除正则. Or you can match it with a \w .(' Hello World Hello Liu ', ' Hello World ', ' Hello Liu ')#############################################################正则对象 Search () methodThis method is used to find strings that can match a successful string. Try to match pattern from the POS subscript of string, if the pattern ends with an acknowledgement match, returns a match object: if it does not match, then retry the match after adding 1 to the POS and instruct Pos=endpos to still not match Fan Hu noneB = ' Aahello world Hello Liu ' reg = Re.compile (R ' ((Hello w.*) (hello l.* ))RESULT4 = Reg.search (b)print result4 print result4.groups () >>><_sre. Sre_match object at 0x037993e0>(' Hello World Hello Liu ', ' Hello World ', ' Hello Liu ')

Python base 8.3 Match method and search method

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.