📜 ⬆️ ⬇️

Regular expressions. Collection of recipes

Every day we work with the text, solving different problems. We check the text for the correctness of the input of some data, look for, replace some values, select some data from the text. Sometimes the volume of this data increases significantly and it is impossible to cope with such volumes of textual information for the current period of time.

We will come to the aid of regular expressions. Many specialists have been using this tool for a long time very successfully. I am talking not only about software developers, but also about people from other professions who have to work with text (editors, marketers, copywriters).

Today I would like to introduce you to the book by Jan Goyvertts and Stephen Levitan “Regular expressions. Collection of recipes, which will help you understand how to work with regular expressions.
')
Let's figure out what kind of a useful tool these regular expressions are.

Regular expressions (English regular expressions, abbr. RegExp, RegEx, jarg. Regekspy or regex) - a system of syntactic analysis of text fragments on a formalized pattern, based on the system of writing patterns for search. The pattern (English pattern), which sets the search rule, is also sometimes called in Russian “pattern”, “mask”.

Wikipedia

It is a little difficult to understand from the description what it is, but I will try to explain with a simple language. We have text that needs to be processed and there is a requirement for how we need to process it. On demand we build some template. We transfer our template and the text to the program which will make analysis. So much easier?

About the book


The book shows solutions based on the use of regular expressions, applied to a wide range of practical problems associated with text processing, arising in various applications.

The book is divided into two large logical parts. The first part is devoted to the theory of regular expressions, syntax and description of different dialects.
Chapter 1. Introduction to regular expressions;
Chapter 2. Basic Regular Expression Skills;
Chapter 3. Programming using regular expressions.

The second part is dedicated to solving various problems that may arise.
Chapter 4. Verification and Formatting;
Chapter 5. Words, strings and special characters;
Chapter 6. Numbers;
Chapter 7. URLs, paths and addresses on the Internet;
Chapter 8. Layout and data exchange.

The book is read very easily and will be understood even by people who have never had programming skills. All examples are built according to the principle - the task is set, it is solved and then it is analyzed in more detail.

About the authors


Ian Goyvertz is the founder of Just Great Software, where he designs and develops some of the most popular regular expression software products. He is the author of RegexBuddy, the world's only regular expression editor that mimics the features of 15 dialects, and PowerGREP, the most powerful grep tool for Microsoft Windows.

Stephen Levitan is a leading regular expressions expert in JavaScript. He runs a popular regular expression blog at http://blog.stevenlevithan.com . Expanding knowledge about dialects of regular expressions and support libraries has been one of his hobbies over the past few years.

Example


I would like to focus on one example in the book. It examines the validation of the input ISBN. I chose it for some reason - it is simple, it is quite visual and, by chance, the example cited in the book written in Python has crept into the error. I decided to rewrite this example.

First, let's make sure the scary pattern ^(?:ISBN(?:-1[03])?:? )?(?=[-0-9 ]{17}$|[-0-9X ]{13}$|[0-9X]{10}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$ really works. I will check in the Espresso text editor.
Espresso regexp
The editor found one match. The template is correct and we can start writing a program in Python (I used version 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"

Check the work of our program, which will check the correctness of the ISBN.
ISBN check

The program works correctly, and a detailed discussion of how this works and some help can be found in the book (§4.13). I just want to add that every recipe that is given in the book is sorted out in detail. Understands the algorithm and the template itself. As an example, let's take a look at this template in the book:

^ #
(?: # ,
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"
$ #


Conclusion


cover Regular expressions are a very powerful tool that can simplify the solution of many problems. This book will help to fully master regular expressions.

“Regular expressions. Collection of recipes "
Ian Goyvertz and Steven Levitan
Symbol-Plus Publishing House, 2010, 608 pages
ISBN 978-5-93286-181-3

Thank you for providing the book to the publishing house " Symbol-Plus ." In the online store publisher book can be purchased .

Source: https://habr.com/ru/post/80430/


All Articles