Ad
2: 2016-12-02 (金) 16:02:27 njf ソース バックアップ No.2 を復元して編集 現: 2017-08-11 (金) 23:32:46 njf ソース 編集
Line 6: Line 6:
 import re  import re
 + 
 testStr = "あいうえお"  testStr = "あいうえお"
 + 
 p = re.compile("あ")  p = re.compile("あ")
 + 
 print p.sub("か",testStr)  print p.sub("か",testStr)
Line 16: Line 16:
 かいうえお  かいうえお
-次に全て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)
Line 31: Line 31:
 かいうえお  かいうえお
-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)
Line 48: Line 48:
 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)
Line 63: Line 63:
これはエラーにはならないので、結構見つけにくいバグになることがあります。 これはエラーにはならないので、結構見つけにくいバグになることがあります。
かならず同じ型でそろえるようにしましょう。 かならず同じ型でそろえるようにしましょう。
-日本語があるなら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)
Line 92: Line 92:
 # -*- 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
Line 112: Line 112:
 # -*- 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)
Line 139: Line 139:
 # -*- 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"
Line 162: Line 162:
 # -*- 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() #検索文字列
Line 185: Line 185:
 にわ  にわ
 にわ  にわ
 + 
 にわ  にわ
 0 2  0 2


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード印刷に適した表示   ページ新規作成 全ページ一覧 単語検索 最新ページの一覧   ヘルプ   最新ページのRSS 1.0 最新ページのRSS 2.0 最新ページのRSS Atom Powered by xpWiki
Counter: 3314, today: 1, yesterday: 0
MenuBar
広告

ログイン

ユーザー名:


パスワード:





パスワード紛失

Portuguese | English | German | Greek | Japanese | Korean | Russian | T-Chinese top
NJF