Python regular expression grouping application-Python tutorial

Source: Internet
Author: User
The group is identified by & quot; (& quot; and & quot;) & quot; metacharacters. & Quot; (& quot; and & quot;) & quot; many of them share the same meaning in mathematical expressions. they combine the expressions in them into a group. The code is as follows:


Import re
Text='v10}renow.android.2.2.normal.1.alpha.apk? IMSI = 460029353813976 & MOBILE = & OLD_VERSION = renow. Android.2.1.Normal. 1. Alpha & CHANNELID = 3'
M = re. search (r "\ w + _ (renow. *) \. (apk | vir | ipa )\? IMSI = (\ d +) & MOBILE = & OLD_VERSION = (. *) & CHANNELID = (. *) ", text)
If m:
Print m. group (0), '\ n', m. group (1), '\ n', m. group (2), '\ n', m. group (3), '\ n', m. group (4), '\ n', m. group (5)
Else:
Print 'not match'



The following is a supplement:

Groups are identified by "(" and ")" metacharacters. "(" And ")" has a lot of the same meanings in mathematical expressions; they combine the expressions in them into a group. For example, you can use repeated delimiters, such as *, + ,?, And {m, n} to repeat the content in the group. for example, (AB) * matches zero or more duplicate "AB ".

Example: If no parentheses are introduced, add an expression as a group, which is group (0)

>>> Import re
>>> P = re. compile ('\ d-\ d ')
>>> M = p. match ('2-3-1 ')
>>> M. groups ()
()
>>> M. group ()
'2-3-1'
>>> M. group (1)
Traceback (most recent call last ):
File" ", Line 1, in
IndexError: no such group

If parentheses are introduced, you can divide the above expressions into three groups, as shown below:

>>> P = re. compile ('(\ d)-(\ d )')
>>> M = p. match ('1-2-3 ')
>>> M. group ()
'1-2-3'
>>> M. group (1)
'1'
>>> M. group (0, 2, 1)
('2-3-1 ', '2', '1 ')

You can also name each group. for example, you can name the first array first.


>>> P = re. compile ('(? P \ D)-(\ d )')
>>> M = p. match ('1-2-3 ')
>>> M. group (1)
'1'
>>> M. group ('first ')

'1'

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.