2: 2016-12-02 (金) 16:02:27 njf  |
現: 2017-08-12 (土) 00:32:46 njf  |
| | | | |
| | import re | | import re |
| | + | |
| | testStr = "あいうえお" | | testStr = "あいうえお" |
| | + | |
| | p = re.compile("あ") | | p = re.compile("あ") |
| | + | |
| | print p.sub("か",testStr) | | print p.sub("か",testStr) |
| | | | |
| | かいうえお | | かいうえお |
| | | | |
| - | 次に全てUTF8でもうまくいきます。 | + | 次に全てunicodeでもうまくいきます。 |
| | | | |
| | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
| | + | |
| | import re | | import re |
| | + | |
| | testStr = u"あいうえお" | | testStr = u"あいうえお" |
| | + | |
| | p = re.compile(u"あ") | | p = re.compile(u"あ") |
| | + | |
| | print p.sub(u"か",testStr) | | print p.sub(u"か",testStr) |
| | | | |
| | かいうえお | | かいうえお |
| | | | |
| - | UTF8とstr型を一つにまとめようとするとエラーになります。 | + | unicodeとstr型を一つにまとめようとするとエラーになります。 |
| | | | |
| | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
| | import re | | import re |
| | + | |
| | testStr = u"あいうえお" | | testStr = u"あいうえお" |
| | + | |
| | p = re.compile(u"あ") | | p = re.compile(u"あ") |
| | + | |
| | print p.sub("か",testStr) | | print p.sub("か",testStr) |
| | | | |
| | UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128) | | UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128) |
| | | | |
| - | UTF8型からstr型を検索したり、またはその逆をすると検索されません。 | + | unicode型からstr型を検索したり、またはその逆をすると検索されません。 |
| | | | |
| | import re | | import re |
| | + | |
| | testStr = "あいうえお" #str | | testStr = "あいうえお" #str |
| - | | + | |
| - | p = re.compile(u"あ") #UTF8 | + | p = re.compile(u"あ") #unicode |
| | + | |
| | print p.sub("か",testStr) | | print p.sub("か",testStr) |
| | | | |
| | これはエラーにはならないので、結構見つけにくいバグになることがあります。 | | これはエラーにはならないので、結構見つけにくいバグになることがあります。 |
| | かならず同じ型でそろえるようにしましょう。 | | かならず同じ型でそろえるようにしましょう。 |
| - | 日本語があるならUTF8にそろえるのがおすすめです。 | + | 日本語があるならunicodeにそろえるのがおすすめです。 |
| | | | |
| | また正規表現検索のメソッドは基本的にre.compleでオブジェクトを作るのと、reからメソッドを呼び出す方法と二つのやり方で実行できます。 | | また正規表現検索のメソッドは基本的にre.compleでオブジェクトを作るのと、reからメソッドを呼び出す方法と二つのやり方で実行できます。 |
| | | | |
| | import re | | import re |
| | + | |
| | testStr = u"あいうえお" | | testStr = u"あいうえお" |
| | + | |
| | p = re.compile(u"あ") | | p = re.compile(u"あ") |
| | + | |
| | print p.sub(u"か",testStr) | | print p.sub(u"か",testStr) |
| | + | |
| | print re.sub(u"あ", u"か", testStr) | | print re.sub(u"あ", u"か", testStr) |
| | | | |
| | | | |
| | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
| | + | |
| | import re | | import re |
| | + | |
| | testStr = u"あいあいうえお" | | testStr = u"あいあいうえお" |
| | + | |
| | p = re.compile(u"あ") | | p = re.compile(u"あ") |
| | + | |
| | print p.sub(u"か",testStr) | | print p.sub(u"か",testStr) |
| | + | |
| | for n in p.subn( u"か", testStr): | | for n in p.subn( u"か", testStr): |
| | print n | | print n |
| | | | |
| | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
| | + | |
| | import re | | import re |
| | + | |
| | testStr = u"あいあいうえお" | | testStr = u"あいあいうえお" |
| | + | |
| | p = re.compile(u"(.)あ") | | p = re.compile(u"(.)あ") |
| | + | |
| | print p.sub(u'\\1か',testStr) | | print p.sub(u'\\1か',testStr) |
| | | | |
| | | | |
| | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
| | + | |
| | import re | | import re |
| | + | |
| | testStr = u"あいうえお" | | testStr = u"あいうえお" |
| | + | |
| | checkStr = u"うえお" | | checkStr = u"うえお" |
| | + | |
| | if re.search(checkStr, testStr): | | if re.search(checkStr, testStr): |
| | print "searched" | | print "searched" |
| | + | |
| | if re.match(checkStr, testStr): | | if re.match(checkStr, testStr): |
| | print "matched" | | print "matched" |
| | | | |
| | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
| | + | |
| | import re | | import re |
| | + | |
| | testStr = u"にわにはにわにわとりがいる" | | testStr = u"にわにはにわにわとりがいる" |
| | + | |
| | checkStr = u"にわ" | | checkStr = u"にわ" |
| | + | |
| | resultList = re.findall(checkStr,testStr) | | resultList = re.findall(checkStr,testStr) |
| | + | |
| | for r in resultList: | | for r in resultList: |
| | print r | | print r |
| | + | |
| | print | | print |
| | + | |
| | for r in re.finditer(checkStr,testStr): | | for r in re.finditer(checkStr,testStr): |
| | print r.group() #検索文字列 | | print r.group() #検索文字列 |
| | にわ | | にわ |
| | にわ | | にわ |
| | + | |
| | にわ | | にわ |
| | 0 2 | | 0 2 |