Pythonの正規表現を詳しく理解する:(?P...)namedグループに名前を付けたグループ

5878 ワード

Python 2.7のマニュアルの説明:
(?P...)
Similar to regular parentheses, but the substring matched by the group is accessible within the rest of the regular expression via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named. So the group named id in the example below can also be referenced as the numbered group 1.
For example, if the pattern is (?P[a-zA-Z_]\w*), the group can be referenced by its name in arguments to methods of match objects, such asm.group('id') or m.end('id'), and also by name in the regular expression itself (using (?P=id)) and replacement text given to .sub() (using \g).
ここでは、上記の原文の意味を詳しく説明します.
声明:ここではできるだけグループを「グループ」と呼び、漢字の「グループ」に訳さず、グループ自体のグループの意味を理解しやすく強化します.
 
1.ここの(?P...)、および普通の(?...):
【チュートリアル】Pythonの正規表現を詳しく理解する:(...)groupパケット
基本的に似ています.違いは、ここではこのグループに名前が付けられているので、後続(同じ正規表現内と検索後に得られるMatchオブジェクト)は、このグループの名前でこのグループを参照することができます.
2.groupの名前は、現在は通常のPython識別子、すなわちアルファベット、数字、下線など、特殊な文字がない必要があります.
3.同じ正規表現内で、groupごとのグループ名は、一意であり、繰り返すことはできません.
4.ここでgroup内に名前が付けられていますが、通常の
【チュートリアル】Pythonの正規表現を詳しく理解する:(...)groupパケット
のように、インデックス番号、すなわちgroup(1)、group(2)などで、対応するgroupを参照することができます.
明らかに、正則内に命名されたグループの順に、順次
group(1)==group(name1)
group(2)==group(name2)
….
 
次に、プレゼンテーション用のサンプルコード、namedグループの使い方を整理します.その中には、比較的複雑なものも含まれています.
(1)名前を付けたgroupは,現在の正規表現ではその後(?P=name)に引用される.
詳細については、以下を参照してください.
【チュートリアル】Pythonの正規表現を詳しく説明する:(?P=name)match earlier named groupは、前に命名されたグループに一致する
(2)re.sub()の後はgで参照されます.
 
サンプルコードは次のとおりです.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 #!/usr/bin/python # -*- coding: utf-8 -*- """ 【 】 Python : (?P...) named group
  http://www.crifan.com/detailed_explanation_about_python_named_group
  Version:    2012-11-12 Author:     Crifan """
  import   re;
  # , , group 。 # , , group 。 # , : #【 】 Python : (…) group #http://www.crifan.com/detailed_explanation_about_python_regular_express_about_group/
  #  http://janexyy.blogbus.com/logs/105359907.html  html # , : #http://code.google.com/p/blogs-to-wordpress/source/browse/trunk/libs/crifan/blogModules/BlogBlogbus.py # -> extractTags() reNamedGroupTestStr  =   u ' :カップルの ' ;
  # 1. (?P=name) # (?P=name) , , , tagName group foundTagA  =   re.search(u '.+?(?P=tagName)' , reNamedGroupTestStr); print   "foundTagA=" ,foundTagA;  #foundTagA= <_sre.sre_match object="" at=""/>
  if (foundTagA):      # 2. mateched.group(name)      # group ,      namedGroupStr  =   foundTagA.group( "tagName" );      print   "namedGroupStr=" ,namedGroupStr;  #namedGroupStr=
       # 3. matched.group(N) == matched.group(name)      # , group index, group(1),group(2),...      # name group      # , ,      # , , , group index      group1Value  =   foundTagA.group( 1 );      print   "Group(1): group1Value=" ,group1Value;  #Group(1): group1Value=             # 4. \g in re.sub()      # re.sub() , \g , name group      substitutedStr  =   re.sub(u '.+?(?P=tagName)' , u ' tag :\g' , reNamedGroupTestStr);      print   "Original string=%s, after substituted=%s" % (reNamedGroupTestStr, substitutedStr);  #Original string= :カップルの , after substituted= tag :
 
【まとめ】
1.一般的にはmatched.group(name)は、検索された文字列に対応するものを取得する.
2.一致する前に表示された文字列については、次の操作を行います.
(1)re.searchなどでは、(?P=name);
(2)re.subの置換文字列では、前に命名されたgroupの値をgで取得します.