正規表現。 レシピ集

毎日、私たちはさまざまなタスクを解決しながらテキストを使用しています。 一部のデータの正しい入力についてテキストをチェックし、それを探し、いくつかの値を置き換え、テキストからいくつかのデータを選択します。 このデータの量が大幅に増加する場合があり、現在の期間、このような量のテキスト情報に対処することは不可能です。



正規表現が助けになります。 多くの専門家がこの強力なツールを長い間非常にうまく使用しています。 ソフトウェア開発者だけでなく、テキストを扱う必要のある他の専門職の人々(編集者、マーケティング担当者、コピーライター)についても話しています。



今日は、ヤンゴーバーツとスティーブンレヴィタンの本「正規表現」を紹介したいと思います。 レシピ集」。正規表現の使用方法を理解するのに役立ちます。



これらの正規表現がどのような便利なツールであるかを見てみましょう。



正規表現(英語の正規表現、略してRegExp、RegEx、jargon。正規表現または正規表現)-検索用のサンプルを記録するシステムに基づいて、正式なテンプレートに従ってテキストフラグメントを解析するシステム。 検索ルールを定義するパターン(英語のパターン)は、ロシア語では「テンプレート」、「マスク」とも呼ばれます。



ウィキペディア



説明からそれが何であるかを理解するのは少し難しいですが、私は簡単な言葉で説明しようとします。 処理する必要のあるテキストがあり、それを処理する方法の要件があります。 リクエストに応じて、テンプレートを作成します。 テンプレートとテキストを解析するプログラムに渡します。 とても簡単ですか?



本について



この本は、さまざまなアプリケーションで発生するワードプロセッシングに関連する幅広い実用的なタスクに適用される、正規表現の使用に基づくソリューションを示しています。



この本は、2つの大きな論理部分に分かれているようです。 最初の部分は、正規表現の理論、さまざまな方言の構文と説明に専念します。

第1章正規表現の概要。

第2章正規表現の基本スキル。

第3章正規表現を使用したプログラミング。



2番目の部分は、発生する可能性のあるさまざまな問題の解決に専念します。

第4章検証とフォーマット。

第5章単語、行、および特殊文字。

第6章数字;

第7章インターネットURL、パス、およびアドレス。

第8章マークアップとデータ交換。



この本は非常に読みやすく、プログラミングスキルのない人でも理解できます。 すべての例は原則に基づいて構築されています-タスクが提示され、解決され、さらに詳細に分析されます。



著者について



Jan GovertsはJust Great Softwareの創設者であり、最も人気のある正規表現製品のいくつかを設計および開発しています。 彼は、15の方言の機能を模倣する世界で唯一の正規表現エディターであるRegexBuddyと、Microsoft Windows用の最も強力なgrepツールであるPowerGREPの著者です。



Stephen Levitanは、JavaScriptの正規表現の第一人者です。 彼はhttp://blog.stevenlevithan.comで人気のある正規表現ブログを維持しています。 正規表現の方言とサポートライブラリの知識を広げることは、ここ数年の趣味の1つです。





私は本の中で一つの例について説明したいと思います。 ISBN入力の検証に対処します。 私は何らかの理由でそれを選びました-それはシンプルで、非常に視覚的で、偶然、Pythonで書かれた本の例として、エラーが入り込んでいます。 この例を書き直すことにしました。



まず、怖いテンプレートを確認しましょう^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$



本当に機能します。 テキストエディターのエスプレッソをチェックインします。

エスプレッソ正規表現

エディターは1つの一致を見つけました。 テンプレートは正しく、Pythonでプログラムの作成を開始できます(バージョン2.6.1を使用しました)。



  1. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  2. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  3. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  4. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  5. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  6. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  7. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  8. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  9. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  10. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  11. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  12. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  13. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  14. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  15. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  16. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  17. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  18. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  19. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  20. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  21. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  22. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  23. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  24. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  25. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  26. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  27. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  28. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  29. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  30. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  31. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  32. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  33. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  34. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  35. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  36. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"



  37. # -*- coding: utf-8 -*- # file: isbn_check.py import re import sys # ISBN-10 ISBN-13 regex = re . compile ( "^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$" ) subject = sys . argv [ 1 ] if regex. search ( subject ) : # , ISBN cbuf = re . sub ( "[^0-9X]" , "" , subject ) # chars = list ( ) # for item in cbuf [ : ] : chars. append ( item ) # chars last last = chars. pop ( ) if len ( chars ) == 9 : # ISBN-10 val = sum ( ( x + 2 ) * int ( y ) for x, y in enumerate ( reversed ( chars ) ) ) check = 11 - ( val % 11 ) if check == 10 : check = "X" elif check == 11 : check = "0" else : # ISBN-13 val = sum ( ( x % 2 * 2 + 1 ) * int ( y ) for x, y in enumerate ( chars ) ) check = 10 - ( val % 10 ) if check == 10 : check = "0" if ( str ( check ) == last ) : print u " ISBN" else : print u " ISBN" else : print u " ISBN"





プログラムの動作を確認し、ISBNの正確性を確認します。

ISBNチェック



プログラムは正常に動作します。この動作の詳細といくつかのヘルプについては、本(§4.13)を参照してください。 本に記載されている各レシピが詳細に分析されることを追加したいだけです。 アルゴリズムとテンプレート自体を理解しています。 例として、本の中でこのテンプレートを見てみましょう:



^ #

(?: # ,

ISBN # ISBN

(?:-1[03])? # "-10" "-13"

:? # ":"

\ # ()

)? #

(?= #

[-0-9 ]{17}$ # 17 ,

| # .

[-0-9X ]{13}$ # 13 , , "X"

| # .

[0-9X]{10}$ # 10 "X",

) #

(?: # ,

97[89] # "978" "979"

[-\ ]? # "-" ()

)? #

[0-9]{1,5} # 1 5

[-\ ]? # "-" ()

(?: # ,

[0-9]+ # , 1

[-\ ]? # "-" ()

){2} # 2

[0-9X] # "X"

$ #








おわりに



カバー 正規表現は、多くのタスクを簡素化できる非常に強力なツールです。 この本は、正規表現を完全にマスターするのに役立ちます。



「正規表現。 レシピ集»

イアン・ゴーバーツとスティーブン・レビタン

Symbol Plus Publishing House、2010、608ページ

ISBN 978-5-93286-181-3



Simvol Plusが提供する本をありがとう。 出版社のオンラインストアから書籍を購入できます。



All Articles