正規表現

なんかなぁ・・・.
正規表現するなら,Perlだろ?って気もしますが,スクリプト言語ということで,正規表現に挑戦です.

まず検索

str = 'test'
p = re.compile(r'.+')
m = p.match(str)
if m:
print m.group(0)

実行結果は,

test

こうなる.group(0)は,検索結果を示す.
検索結果として欲しいのは,タグの中身なので,それをこのgroupを使って取り出してみる.

str = 'test'
p = re.compile(r'(.+)')
m = p.match(str)
if m:
print m.group(1)

実行結果は,

test

こうなる.ますます,Perlでいい気がしてきた・・・.

文字列の置換

import re
srcLine = 'hello&world'
srcLine = re.sub(r'&', r'&', srcLine)
print srcLine

こうすると,結果は,

hello&world

と出力される.