Python regular expression match count FindAll usage

Source: Internet
Author: User

The number of matches is defined with {}


Re.search can generate group () that can access the string for each group

The result of Re.findall is a list of elements that can be a string or a tuple

microsoft windows [version  6.1.7601] copyright   (c)  2009 microsoft corporation. All rights reserved. c:\users\user>pythonpython 3.5.2 | anaconda 4.2.0  (64-bit) |  (default, jul  5 2016, 11:41:13)  [MSC  v.1900 64 bit  (AMD64)] on win32type  "Help",  "copyright",  " Credits " or " license " for more information.>>> import re>> > str= "ATGTACGTACGTACTTTCCGT" >>> pat_str=re.compile ("(GTAC) {1}") >>> na  = re.search (pat_str,x) >>> na<_sre. Sre_match object; span= (2, 6),  match= ' GTAC ' >>>> na.group (1) ' GTAC ' >> > na.group (2) traceback  (most recent call last):  file  "<stdin > ", line 1, in <module>indexerror: no such group>>>  Na.group (0) ' GTAC ' >>>&Nbsp;for i in na.group ():...     print  (i) ... gtac>>> str_x= "Atgtacgtacgtactttccgtttccct" >>> str_y=re.compile ("(GTAC) {1}[GTAC]{5 } ") >>> na = re.search (str_y, str_x) >>> na.group () ' GTACGTACG ' > >> x= "Atgtacgtacgtactttccgtttccctgtaccccggttaat" >>> y=re.compile ("(GTAC) {1}[GTAC]{5}" ) >>> na = re.search (y,x) >>> na.group () ' GTACGTACG ' >>>  Na.group (1) ' GTAC ' >>> nas = re.findall (y, x) >>> nas[' GTAC ',  ' GTAC ']>>> re.sub (y, "nnnnnnnnn",  x) ' Atnnnnnnnnntactttccgtttccctnnnnnnnnnttaat ' >>>  x ' Atgtacgtacgtactttccgtttccctgtaccccggttaat ' >>> y=re.compile ("(GTAC) {1}[GTAC]{5}",  Flags=re. I) >>> nag=re.search (y,x) >>> nag.group () ' GTACGTACG ' >>> for i  in na.group (): ...      print  (i) ... Gtacgtacg>>> for i in na.group (1):...     print  (i) ... gtac#  use  flags=re when setting flags  and  search when using compile. The i  effect is the same as >>> na_re_i = re.search (r "(GTAC) {1}[gtac]{5}",  x, flags=re. I) >>> na_re_i<_sre. Sre_match object; span= (2, 11),  match= ' GTACGTACG ' >>>> na_re_i.group () ' GTACGTACG ' >>> na_re_i.group (1) ' GTAC ' >>> y.findall (x) [' GTAC ',  ' GTAC ']>> > y_all=re.compile ("((GTAC) {1}[gtac]{5})",  flags=re. I) >>> nas_all = re.findall (y_all, x) >>> nas_all[(' GTACGTACG ',  ' GTAC '),  (' GTACCCCGG ',  ' GTAC ')]>>>


FindAll Effect Reference:

http://blog.csdn.net/cashey1991/article/details/8875213

http://blog.163.com/[email protected]/blog/static/35265851201672431418177/


This article is from the "R and Python apps" blog, so be sure to keep this source http://matrix6ro.blog.51cto.com/1746429/1875383

Python regular expression match count FindAll usage

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.