LoginSignup
2
1

More than 5 years have passed since last update.

全ての形容動詞と、その位置を見つける

Last updated at Posted at 2013-02-25

こういうの無いかなあと思って調べたらそのままあったのでメモ。

もし、パターンにマッチするものについて、マッチしたテキスト以上の情報を得たいと考えたとき、文字列ではなく MatchObject のインスタンスを返す finditer() が便利です。以下に例を示すように、なにかの文章の全ての副詞と、 その位置を 調べたいと考えたとき、下記のように finditer() を使います。 :

>>> text = "He was carefully disguised but captured quickly by police."
>>> for m in re.finditer(r"\w+ly", text):
...     print '%02d-%02d: %s' % (m.start(), m.end(), m.group(0))
07-16: carefully
40-47: quickly

日本語を含むテキストの場合、decodeを使ってunicodeへ変換する。

text = "これはテストです"
regex = "テスト"
a = re.finditer(regex.decode('utf8'), text.decode('utf8'))
for i in a: print i.group(), i.start(), i.end()

結果は、

テスト 3 6

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1