Mar 27, 2006
[Misc] Named Capture メモ
最速インターフェース研究会さんのNamedCapture for JavaScriptでNamed Captureというものを知った。 日本語では「名前付きキャプチャ」と言うらしい。 Named Capture は正規表現の Syntax で、(?P<name>regexp) と言うように名前を指定することで今までマッチした順序を数値で指定していたところが名前でアクセスできるようになるらしい。 細かい改良だけれど、意外と便利そう。
- .NET での正規表現の使用
- http://japan.internet.com/developer/20050822/28.html
- グループ化構成体
- http://msdn.microsoft.com/library/ja/cpgenref/html/cpcongroupingconstructs.asp?frame=true
- Named Capture Groups
- http://regular-expressions.info/named.html
The regular expression classes of the .NET framework also support named capture. Unfortunately, the Microsoft developers decided to invent their own syntax, rather than follow the one pioneered by Python.
Python で Named Capture にチャレンジ
$ python
Python 1.5.2 (#1, Jan 31 2003, 11:01:49) [GCC 2.96 20000731 (Red Hat Linux 7.2 2 on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import re
>>> search = re.compile("(?P[a-zA-Z]+)")
>>> line = "123abc456"
>>> result = search.search(line)
>>> print result.group('alphabet')
abc
>>> print result.group(0)
abc
>>>



