import PrintHelper
PrintHelper.PrintTitle('Regular Expressions With Python')
print('适用于数据挖掘,数据处理,抓取网页数据,大量的数据集合的模式化匹配')
print('正则表达式是字符串的模式化匹配,本章介绍如何使用正则表达式进行匹配')
PrintHelper.PrintSubtitle('The re Module')
PrintHelper.PrintHint('re,正则表达式模块')
PrintHelper.PrintHint('@pattern=re.compile(r@pattenString)','编译pattern')
PrintHelper.PrintHint('@pattern.findall(@stringForMatch)','使用pattern对象进行字符串匹配')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pAplus = re.compile( r\"a+\" )')
PrintHelper.PrintCode('lAplus = pAplus.findall( \"aardvark\" )')
PrintHelper.PrintCode('print( lAplus )')
import re
pAplus = re.compile( r"a+" )
lAplus = pAplus.findall( "aardvark" )
print( lAplus )
PrintHelper.PrintSubtitle('Shorthand,简写')
PrintHelper.PrintHint('re.findall(r@patternString,@stringForMatch)','直接使用patternString进行字符串匹配')
PrintHelper.PrintSubtitle('Match Objects')
print('当你需要更多的匹配信息的时候,re模块提供了一种称为MatchObjects的对象类型')
print('这种对象包含了更多的关于匹配的信息')
PrintHelper.PrintHint('@match=re.search(r@patternString,@stringForMatch)')
PrintHelper.PrintHint('@match.group()','匹配的内容')
PrintHelper.PrintHint('@match.start()','匹配的起始位置')
PrintHelper.PrintHint('@match.end()','匹配的结束位置')
PrintHelper.PrintCode('m = re.search( r\"a+\", \"Look out for the aardvark!\" )')
PrintHelper.PrintCode('print( \"{} is found at index {}\".format( m.group(), m.start() ) )')
m = re.search( r"a+", "Look out for the aardvark!" )
print( "{} is found at index {}".format( m.group(), m.start() ) )
PrintHelper.PrintSubtitle('Lists Of Matches',"多项匹配结果")
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('mlist = re.finditer( r\"a+\", \"Look out! A dangerous aardvark is on the loose!\" )')
PrintHelper.PrintCode('for m in mlist:')
PrintHelper.PrintCode('    print( \"{} is found at index {} and ends at index {}.\".format( m.group(), m.start(), m.end())) ')
import re
mlist = re.finditer( r"a+", "Look out! A dangerous aardvark is on the loose!" )
for m in mlist:
    print( "{} is found at index {} and ends at index {}.".format( m.group(), m.start(), m.end())) 
PrintHelper.PrintTitle('Writing Regular Expressions')
PrintHelper.PrintSubtitle('Regular Expressions With Square Brackets')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('slist = re.findall( r\"b[aeiou]ll\", \"Bill Gates and Uwe Boll \\ drank Red Bull at a football match in Campbell.\" )')
PrintHelper.PrintCode('print( slist )')
import re
slist = re.findall( r"b[aeiou]ll", "Bill Gates and Uwe Boll \ drank Red Bull at a football match in Campbell." )
print( slist )
PrintHelper.PrintSubtitle('Special Sequences,特殊序列标记')
PrintHelper.PrintSampleWithDescription('\\b','word boundary,单词边界')
PrintHelper.PrintSampleWithDescription('\\B','not a word boundary,非单次边界')
PrintHelper.PrintSampleWithDescription('\\d','digit,数字')
PrintHelper.PrintSampleWithDescription('\\D','not a digit,非数字')
PrintHelper.PrintSampleWithDescription('\\n','newline,换行')
PrintHelper.PrintSampleWithDescription('\\r','carriage return,回车')
PrintHelper.PrintSampleWithDescription('\\s','whitespace,空格')
PrintHelper.PrintSampleWithDescription('\\S','not a whitespace,非空格')
PrintHelper.PrintSampleWithDescription('\\t','tabulation,缩进')
PrintHelper.PrintSampleWithDescription('\\w','alphanumeric character,数字+字母')
PrintHelper.PrintSampleWithDescription('\\W','not an alphanumeric character,非数字和字母')
PrintHelper.PrintSampleWithDescription('\\/','forward slash,斜杠,左下斜杠')
PrintHelper.PrintSampleWithDescription('\\\\','backslash,反斜杠,右下反斜杠')
PrintHelper.PrintSampleWithDescription('\\\"','double quote,双眼号')
PrintHelper.PrintSampleWithDescription('\\\'','single quote,单眼号')
PrintHelper.PrintSampleWithDescription('-','start of a string,字符串头')
PrintHelper.PrintSampleWithDescription('$','end of a string,字符串尾')
PrintHelper.PrintSampleWithDescription('.','any character,任意字符')
PrintHelper.PrintSubtitle('Repetition,重复标记')
PrintHelper.PrintSampleWithDescription('*','zero or more,0次或更多')
PrintHelper.PrintSampleWithDescription('+','one or more,至少一次')
PrintHelper.PrintSampleWithDescription('?','zero or one,可能出现一次')
PrintHelper.PrintSampleWithDescription('{n,m}','at least n and at most m,至少n次,最多m次')
PrintHelper.PrintSampleWithDescription('{n,}','at least n,至少n次')
PrintHelper.PrintSampleWithDescription('{n}','exactly n,正好n次')
PrintHelper.PrintTitle('Grouping,匹配分组')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r\"(\\d{1,2})-(\\d{1,2})-(\\d{4})\" )')
PrintHelper.PrintCode('m = pDate.search( \"In response to your letter of 25-3-2015, \\ I decided to hire a hitman to get you.\" )')
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( \"Date {}; day {}; month {}; year {}\"')
PrintHelper.PrintCode('           .format( m.group(0), m.group(1), m.group(2), m.group(3) ) )')
import re
pDate = re.compile( r"(\d{1,2})-(\d{1,2})-(\d{4})" )
m = pDate.search( "In response to your letter of 25-3-2015, \ I decided to hire a hitman to get you." )
if m:
    print( "Date {}; day {}; month {}; year {}"
           .format( m.group(0), m.group(1), m.group(2), m.group(3) ) )
PrintHelper.PrintSubtitle('findall() and Groups')
PrintHelper.PrintHint('findall(@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r\"(\\d{1,2})-(\\d{1,2})-(\\d{4})\" )')
PrintHelper.PrintCode('datelist = pDate.findall( \"In response to your letter of \\ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you.\" )')
PrintHelper.PrintCode('for date in datelist:')
PrintHelper.PrintCode('    print( date )')
import re
pDate = re.compile( r"(\d{1,2})-(\d{1,2})-(\d{4})" )
datelist = pDate.findall( "In response to your letter of \ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you." )
for date in datelist:
    print( date )
PrintHelper.PrintSubtitle('Named Groups,组命名')
PrintHelper.PrintHint('(?P<@name>...)')
PrintHelper.PrintCode('pDate = re.compile( r\"(?P<day>\\d{1,2})-(?P<month>\\d{1,2})-(?P<year>\\d{4})\")')
PrintHelper.PrintCode('m = pDate.search( \"In response to your letter of 25-3-2015, \\ I curse you.\" )')
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( \"day is {}\".format( m.group( \'day\') ) )')
PrintHelper.PrintCode('    print( \"month is {}\".format( m.group( \'month\') ) )')
PrintHelper.PrintCode('    print( \"year is {}\".format( m.group( \'year\') ) )')
pDate = re.compile( r"(?P<day>\d{1,2})-(?P<month>\d{1,2})-(?P<year>\d{4})")
m = pDate.search( "In response to your letter of 25-3-2015, \ I curse you." )
if m:
    print( "day is {}".format( m.group( 'day') ) )
    print( "month is {}".format( m.group( 'month') ) )
    print( "year is {}".format( m.group( 'year') ) )
PrintHelper.PrintSubtitle('Referring Within A Regular Expression,匹配内引用')
PrintHelper.PrintHint('\\n,如(\\S).*\\1指代任意非空格的字符,且重复两次,\\1指第一个匹配项')
PrintHelper.PrintTitle('Replacing,替换')
print('正则通常用以匹配内容,当然你也可以使用正则进行替换处理')
PrintHelper.PrintHint('re.sub(r@pattern,@replace,@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('s = re.sub( r\"([iy])se\", \"\\g<1>ze\", \"Whether you categorise, \\ emphasise, or analyse, you should use American spelling!\" )')
PrintHelper.PrintCode('print( s )')
import re
s = re.sub( r"([iy])se", "\g<1>ze", "Whether you categorise, \ emphasise, or analyse, you should use American spelling!" )
print( s )
print()


2168445a69fd58027a1d22b32431a5f3569f52c3
<mask token>

<mask token>
PrintHelper.PrintTitle('Regular Expressions With Python')
print('适用于数据挖掘,数据处理,抓取网页数据,大量的数据集合的模式化匹配')
print('正则表达式是字符串的模式化匹配,本章介绍如何使用正则表达式进行匹配')
PrintHelper.PrintSubtitle('The re Module')
PrintHelper.PrintHint('re,正则表达式模块')
PrintHelper.PrintHint('@pattern=re.compile(r@pattenString)', '编译pattern')
PrintHelper.PrintHint('@pattern.findall(@stringForMatch)', '使用pattern对象进行字符串匹配'
    )
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pAplus = re.compile( r"a+" )')
PrintHelper.PrintCode('lAplus = pAplus.findall( "aardvark" )')
PrintHelper.PrintCode('print( lAplus )')
<mask token>
print(lAplus)
PrintHelper.PrintSubtitle('Shorthand,简写')
PrintHelper.PrintHint('re.findall(r@patternString,@stringForMatch)',
    '直接使用patternString进行字符串匹配')
PrintHelper.PrintSubtitle('Match Objects')
print('当你需要更多的匹配信息的时候,re模块提供了一种称为MatchObjects的对象类型')
print('这种对象包含了更多的关于匹配的信息')
PrintHelper.PrintHint('@match=re.search(r@patternString,@stringForMatch)')
PrintHelper.PrintHint('@match.group()', '匹配的内容')
PrintHelper.PrintHint('@match.start()', '匹配的起始位置')
PrintHelper.PrintHint('@match.end()', '匹配的结束位置')
PrintHelper.PrintCode('m = re.search( r"a+", "Look out for the aardvark!" )')
PrintHelper.PrintCode(
    'print( "{} is found at index {}".format( m.group(), m.start() ) )')
<mask token>
print('{} is found at index {}'.format(m.group(), m.start()))
PrintHelper.PrintSubtitle('Lists Of Matches', '多项匹配结果')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    'mlist = re.finditer( r"a+", "Look out! A dangerous aardvark is on the loose!" )'
    )
PrintHelper.PrintCode('for m in mlist:')
PrintHelper.PrintCode(
    '    print( "{} is found at index {} and ends at index {}.".format( m.group(), m.start(), m.end())) '
    )
<mask token>
for m in mlist:
    print('{} is found at index {} and ends at index {}.'.format(m.group(),
        m.start(), m.end()))
PrintHelper.PrintTitle('Writing Regular Expressions')
PrintHelper.PrintSubtitle('Regular Expressions With Square Brackets')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    'slist = re.findall( r"b[aeiou]ll", "Bill Gates and Uwe Boll \\ drank Red Bull at a football match in Campbell." )'
    )
PrintHelper.PrintCode('print( slist )')
<mask token>
print(slist)
PrintHelper.PrintSubtitle('Special Sequences,特殊序列标记')
PrintHelper.PrintSampleWithDescription('\\b', 'word boundary,单词边界')
PrintHelper.PrintSampleWithDescription('\\B', 'not a word boundary,非单次边界')
PrintHelper.PrintSampleWithDescription('\\d', 'digit,数字')
PrintHelper.PrintSampleWithDescription('\\D', 'not a digit,非数字')
PrintHelper.PrintSampleWithDescription('\\n', 'newline,换行')
PrintHelper.PrintSampleWithDescription('\\r', 'carriage return,回车')
PrintHelper.PrintSampleWithDescription('\\s', 'whitespace,空格')
PrintHelper.PrintSampleWithDescription('\\S', 'not a whitespace,非空格')
PrintHelper.PrintSampleWithDescription('\\t', 'tabulation,缩进')
PrintHelper.PrintSampleWithDescription('\\w', 'alphanumeric character,数字+字母')
PrintHelper.PrintSampleWithDescription('\\W',
    'not an alphanumeric character,非数字和字母')
PrintHelper.PrintSampleWithDescription('\\/', 'forward slash,斜杠,左下斜杠')
PrintHelper.PrintSampleWithDescription('\\\\', 'backslash,反斜杠,右下反斜杠')
PrintHelper.PrintSampleWithDescription('\\"', 'double quote,双眼号')
PrintHelper.PrintSampleWithDescription("\\'", 'single quote,单眼号')
PrintHelper.PrintSampleWithDescription('-', 'start of a string,字符串头')
PrintHelper.PrintSampleWithDescription('$', 'end of a string,字符串尾')
PrintHelper.PrintSampleWithDescription('.', 'any character,任意字符')
PrintHelper.PrintSubtitle('Repetition,重复标记')
PrintHelper.PrintSampleWithDescription('*', 'zero or more,0次或更多')
PrintHelper.PrintSampleWithDescription('+', 'one or more,至少一次')
PrintHelper.PrintSampleWithDescription('?', 'zero or one,可能出现一次')
PrintHelper.PrintSampleWithDescription('{n,m}',
    'at least n and at most m,至少n次,最多m次')
PrintHelper.PrintSampleWithDescription('{n,}', 'at least n,至少n次')
PrintHelper.PrintSampleWithDescription('{n}', 'exactly n,正好n次')
PrintHelper.PrintTitle('Grouping,匹配分组')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r"(\\d{1,2})-(\\d{1,2})-(\\d{4})" )'
    )
PrintHelper.PrintCode(
    'm = pDate.search( "In response to your letter of 25-3-2015, \\ I decided to hire a hitman to get you." )'
    )
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( "Date {}; day {}; month {}; year {}"')
PrintHelper.PrintCode(
    '           .format( m.group(0), m.group(1), m.group(2), m.group(3) ) )')
<mask token>
if m:
    print('Date {}; day {}; month {}; year {}'.format(m.group(0), m.group(1
        ), m.group(2), m.group(3)))
PrintHelper.PrintSubtitle('findall() and Groups')
PrintHelper.PrintHint('findall(@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r"(\\d{1,2})-(\\d{1,2})-(\\d{4})" )'
    )
PrintHelper.PrintCode(
    'datelist = pDate.findall( "In response to your letter of \\ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you." )'
    )
PrintHelper.PrintCode('for date in datelist:')
PrintHelper.PrintCode('    print( date )')
<mask token>
for date in datelist:
    print(date)
PrintHelper.PrintSubtitle('Named Groups,组命名')
PrintHelper.PrintHint('(?P<@name>...)')
PrintHelper.PrintCode(
    'pDate = re.compile( r"(?P<day>\\d{1,2})-(?P<month>\\d{1,2})-(?P<year>\\d{4})")'
    )
PrintHelper.PrintCode(
    'm = pDate.search( "In response to your letter of 25-3-2015, \\ I curse you." )'
    )
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( "day is {}".format( m.group( \'day\') ) )')
PrintHelper.PrintCode(
    '    print( "month is {}".format( m.group( \'month\') ) )')
PrintHelper.PrintCode('    print( "year is {}".format( m.group( \'year\') ) )')
<mask token>
if m:
    print('day is {}'.format(m.group('day')))
    print('month is {}'.format(m.group('month')))
    print('year is {}'.format(m.group('year')))
PrintHelper.PrintSubtitle('Referring Within A Regular Expression,匹配内引用')
PrintHelper.PrintHint('\\n,如(\\S).*\\1指代任意非空格的字符,且重复两次,\\1指第一个匹配项')
PrintHelper.PrintTitle('Replacing,替换')
print('正则通常用以匹配内容,当然你也可以使用正则进行替换处理')
PrintHelper.PrintHint('re.sub(r@pattern,@replace,@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    's = re.sub( r"([iy])se", "\\g<1>ze", "Whether you categorise, \\ emphasise, or analyse, you should use American spelling!" )'
    )
PrintHelper.PrintCode('print( s )')
<mask token>
print(s)
print()

<mask token>
PrintHelper.PrintTitle('Regular Expressions With Python')
print('适用于数据挖掘,数据处理,抓取网页数据,大量的数据集合的模式化匹配')
print('正则表达式是字符串的模式化匹配,本章介绍如何使用正则表达式进行匹配')
PrintHelper.PrintSubtitle('The re Module')
PrintHelper.PrintHint('re,正则表达式模块')
PrintHelper.PrintHint('@pattern=re.compile(r@pattenString)', '编译pattern')
PrintHelper.PrintHint('@pattern.findall(@stringForMatch)', '使用pattern对象进行字符串匹配'
    )
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pAplus = re.compile( r"a+" )')
PrintHelper.PrintCode('lAplus = pAplus.findall( "aardvark" )')
PrintHelper.PrintCode('print( lAplus )')
<mask token>
pAplus = re.compile('a+')
lAplus = pAplus.findall('aardvark')
print(lAplus)
PrintHelper.PrintSubtitle('Shorthand,简写')
PrintHelper.PrintHint('re.findall(r@patternString,@stringForMatch)',
    '直接使用patternString进行字符串匹配')
PrintHelper.PrintSubtitle('Match Objects')
print('当你需要更多的匹配信息的时候,re模块提供了一种称为MatchObjects的对象类型')
print('这种对象包含了更多的关于匹配的信息')
PrintHelper.PrintHint('@match=re.search(r@patternString,@stringForMatch)')
PrintHelper.PrintHint('@match.group()', '匹配的内容')
PrintHelper.PrintHint('@match.start()', '匹配的起始位置')
PrintHelper.PrintHint('@match.end()', '匹配的结束位置')
PrintHelper.PrintCode('m = re.search( r"a+", "Look out for the aardvark!" )')
PrintHelper.PrintCode(
    'print( "{} is found at index {}".format( m.group(), m.start() ) )')
m = re.search('a+', 'Look out for the aardvark!')
print('{} is found at index {}'.format(m.group(), m.start()))
PrintHelper.PrintSubtitle('Lists Of Matches', '多项匹配结果')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    'mlist = re.finditer( r"a+", "Look out! A dangerous aardvark is on the loose!" )'
    )
PrintHelper.PrintCode('for m in mlist:')
PrintHelper.PrintCode(
    '    print( "{} is found at index {} and ends at index {}.".format( m.group(), m.start(), m.end())) '
    )
<mask token>
mlist = re.finditer('a+', 'Look out! A dangerous aardvark is on the loose!')
for m in mlist:
    print('{} is found at index {} and ends at index {}.'.format(m.group(),
        m.start(), m.end()))
PrintHelper.PrintTitle('Writing Regular Expressions')
PrintHelper.PrintSubtitle('Regular Expressions With Square Brackets')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    'slist = re.findall( r"b[aeiou]ll", "Bill Gates and Uwe Boll \\ drank Red Bull at a football match in Campbell." )'
    )
PrintHelper.PrintCode('print( slist )')
<mask token>
slist = re.findall('b[aeiou]ll',
    'Bill Gates and Uwe Boll \\ drank Red Bull at a football match in Campbell.'
    )
print(slist)
PrintHelper.PrintSubtitle('Special Sequences,特殊序列标记')
PrintHelper.PrintSampleWithDescription('\\b', 'word boundary,单词边界')
PrintHelper.PrintSampleWithDescription('\\B', 'not a word boundary,非单次边界')
PrintHelper.PrintSampleWithDescription('\\d', 'digit,数字')
PrintHelper.PrintSampleWithDescription('\\D', 'not a digit,非数字')
PrintHelper.PrintSampleWithDescription('\\n', 'newline,换行')
PrintHelper.PrintSampleWithDescription('\\r', 'carriage return,回车')
PrintHelper.PrintSampleWithDescription('\\s', 'whitespace,空格')
PrintHelper.PrintSampleWithDescription('\\S', 'not a whitespace,非空格')
PrintHelper.PrintSampleWithDescription('\\t', 'tabulation,缩进')
PrintHelper.PrintSampleWithDescription('\\w', 'alphanumeric character,数字+字母')
PrintHelper.PrintSampleWithDescription('\\W',
    'not an alphanumeric character,非数字和字母')
PrintHelper.PrintSampleWithDescription('\\/', 'forward slash,斜杠,左下斜杠')
PrintHelper.PrintSampleWithDescription('\\\\', 'backslash,反斜杠,右下反斜杠')
PrintHelper.PrintSampleWithDescription('\\"', 'double quote,双眼号')
PrintHelper.PrintSampleWithDescription("\\'", 'single quote,单眼号')
PrintHelper.PrintSampleWithDescription('-', 'start of a string,字符串头')
PrintHelper.PrintSampleWithDescription('$', 'end of a string,字符串尾')
PrintHelper.PrintSampleWithDescription('.', 'any character,任意字符')
PrintHelper.PrintSubtitle('Repetition,重复标记')
PrintHelper.PrintSampleWithDescription('*', 'zero or more,0次或更多')
PrintHelper.PrintSampleWithDescription('+', 'one or more,至少一次')
PrintHelper.PrintSampleWithDescription('?', 'zero or one,可能出现一次')
PrintHelper.PrintSampleWithDescription('{n,m}',
    'at least n and at most m,至少n次,最多m次')
PrintHelper.PrintSampleWithDescription('{n,}', 'at least n,至少n次')
PrintHelper.PrintSampleWithDescription('{n}', 'exactly n,正好n次')
PrintHelper.PrintTitle('Grouping,匹配分组')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r"(\\d{1,2})-(\\d{1,2})-(\\d{4})" )'
    )
PrintHelper.PrintCode(
    'm = pDate.search( "In response to your letter of 25-3-2015, \\ I decided to hire a hitman to get you." )'
    )
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( "Date {}; day {}; month {}; year {}"')
PrintHelper.PrintCode(
    '           .format( m.group(0), m.group(1), m.group(2), m.group(3) ) )')
<mask token>
pDate = re.compile('(\\d{1,2})-(\\d{1,2})-(\\d{4})')
m = pDate.search(
    'In response to your letter of 25-3-2015, \\ I decided to hire a hitman to get you.'
    )
if m:
    print('Date {}; day {}; month {}; year {}'.format(m.group(0), m.group(1
        ), m.group(2), m.group(3)))
PrintHelper.PrintSubtitle('findall() and Groups')
PrintHelper.PrintHint('findall(@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r"(\\d{1,2})-(\\d{1,2})-(\\d{4})" )'
    )
PrintHelper.PrintCode(
    'datelist = pDate.findall( "In response to your letter of \\ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you." )'
    )
PrintHelper.PrintCode('for date in datelist:')
PrintHelper.PrintCode('    print( date )')
<mask token>
pDate = re.compile('(\\d{1,2})-(\\d{1,2})-(\\d{4})')
datelist = pDate.findall(
    'In response to your letter of \\ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you.'
    )
for date in datelist:
    print(date)
PrintHelper.PrintSubtitle('Named Groups,组命名')
PrintHelper.PrintHint('(?P<@name>...)')
PrintHelper.PrintCode(
    'pDate = re.compile( r"(?P<day>\\d{1,2})-(?P<month>\\d{1,2})-(?P<year>\\d{4})")'
    )
PrintHelper.PrintCode(
    'm = pDate.search( "In response to your letter of 25-3-2015, \\ I curse you." )'
    )
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( "day is {}".format( m.group( \'day\') ) )')
PrintHelper.PrintCode(
    '    print( "month is {}".format( m.group( \'month\') ) )')
PrintHelper.PrintCode('    print( "year is {}".format( m.group( \'year\') ) )')
pDate = re.compile('(?P<day>\\d{1,2})-(?P<month>\\d{1,2})-(?P<year>\\d{4})')
m = pDate.search('In response to your letter of 25-3-2015, \\ I curse you.')
if m:
    print('day is {}'.format(m.group('day')))
    print('month is {}'.format(m.group('month')))
    print('year is {}'.format(m.group('year')))
PrintHelper.PrintSubtitle('Referring Within A Regular Expression,匹配内引用')
PrintHelper.PrintHint('\\n,如(\\S).*\\1指代任意非空格的字符,且重复两次,\\1指第一个匹配项')
PrintHelper.PrintTitle('Replacing,替换')
print('正则通常用以匹配内容,当然你也可以使用正则进行替换处理')
PrintHelper.PrintHint('re.sub(r@pattern,@replace,@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    's = re.sub( r"([iy])se", "\\g<1>ze", "Whether you categorise, \\ emphasise, or analyse, you should use American spelling!" )'
    )
PrintHelper.PrintCode('print( s )')
<mask token>
s = re.sub('([iy])se', '\\g<1>ze',
    'Whether you categorise, \\ emphasise, or analyse, you should use American spelling!'
    )
print(s)
print()

import PrintHelper
PrintHelper.PrintTitle('Regular Expressions With Python')
print('适用于数据挖掘,数据处理,抓取网页数据,大量的数据集合的模式化匹配')
print('正则表达式是字符串的模式化匹配,本章介绍如何使用正则表达式进行匹配')
PrintHelper.PrintSubtitle('The re Module')
PrintHelper.PrintHint('re,正则表达式模块')
PrintHelper.PrintHint('@pattern=re.compile(r@pattenString)', '编译pattern')
PrintHelper.PrintHint('@pattern.findall(@stringForMatch)', '使用pattern对象进行字符串匹配'
    )
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pAplus = re.compile( r"a+" )')
PrintHelper.PrintCode('lAplus = pAplus.findall( "aardvark" )')
PrintHelper.PrintCode('print( lAplus )')
import re
pAplus = re.compile('a+')
lAplus = pAplus.findall('aardvark')
print(lAplus)
PrintHelper.PrintSubtitle('Shorthand,简写')
PrintHelper.PrintHint('re.findall(r@patternString,@stringForMatch)',
    '直接使用patternString进行字符串匹配')
PrintHelper.PrintSubtitle('Match Objects')
print('当你需要更多的匹配信息的时候,re模块提供了一种称为MatchObjects的对象类型')
print('这种对象包含了更多的关于匹配的信息')
PrintHelper.PrintHint('@match=re.search(r@patternString,@stringForMatch)')
PrintHelper.PrintHint('@match.group()', '匹配的内容')
PrintHelper.PrintHint('@match.start()', '匹配的起始位置')
PrintHelper.PrintHint('@match.end()', '匹配的结束位置')
PrintHelper.PrintCode('m = re.search( r"a+", "Look out for the aardvark!" )')
PrintHelper.PrintCode(
    'print( "{} is found at index {}".format( m.group(), m.start() ) )')
m = re.search('a+', 'Look out for the aardvark!')
print('{} is found at index {}'.format(m.group(), m.start()))
PrintHelper.PrintSubtitle('Lists Of Matches', '多项匹配结果')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    'mlist = re.finditer( r"a+", "Look out! A dangerous aardvark is on the loose!" )'
    )
PrintHelper.PrintCode('for m in mlist:')
PrintHelper.PrintCode(
    '    print( "{} is found at index {} and ends at index {}.".format( m.group(), m.start(), m.end())) '
    )
import re
mlist = re.finditer('a+', 'Look out! A dangerous aardvark is on the loose!')
for m in mlist:
    print('{} is found at index {} and ends at index {}.'.format(m.group(),
        m.start(), m.end()))
PrintHelper.PrintTitle('Writing Regular Expressions')
PrintHelper.PrintSubtitle('Regular Expressions With Square Brackets')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    'slist = re.findall( r"b[aeiou]ll", "Bill Gates and Uwe Boll \\ drank Red Bull at a football match in Campbell." )'
    )
PrintHelper.PrintCode('print( slist )')
import re
slist = re.findall('b[aeiou]ll',
    'Bill Gates and Uwe Boll \\ drank Red Bull at a football match in Campbell.'
    )
print(slist)
PrintHelper.PrintSubtitle('Special Sequences,特殊序列标记')
PrintHelper.PrintSampleWithDescription('\\b', 'word boundary,单词边界')
PrintHelper.PrintSampleWithDescription('\\B', 'not a word boundary,非单次边界')
PrintHelper.PrintSampleWithDescription('\\d', 'digit,数字')
PrintHelper.PrintSampleWithDescription('\\D', 'not a digit,非数字')
PrintHelper.PrintSampleWithDescription('\\n', 'newline,换行')
PrintHelper.PrintSampleWithDescription('\\r', 'carriage return,回车')
PrintHelper.PrintSampleWithDescription('\\s', 'whitespace,空格')
PrintHelper.PrintSampleWithDescription('\\S', 'not a whitespace,非空格')
PrintHelper.PrintSampleWithDescription('\\t', 'tabulation,缩进')
PrintHelper.PrintSampleWithDescription('\\w', 'alphanumeric character,数字+字母')
PrintHelper.PrintSampleWithDescription('\\W',
    'not an alphanumeric character,非数字和字母')
PrintHelper.PrintSampleWithDescription('\\/', 'forward slash,斜杠,左下斜杠')
PrintHelper.PrintSampleWithDescription('\\\\', 'backslash,反斜杠,右下反斜杠')
PrintHelper.PrintSampleWithDescription('\\"', 'double quote,双眼号')
PrintHelper.PrintSampleWithDescription("\\'", 'single quote,单眼号')
PrintHelper.PrintSampleWithDescription('-', 'start of a string,字符串头')
PrintHelper.PrintSampleWithDescription('$', 'end of a string,字符串尾')
PrintHelper.PrintSampleWithDescription('.', 'any character,任意字符')
PrintHelper.PrintSubtitle('Repetition,重复标记')
PrintHelper.PrintSampleWithDescription('*', 'zero or more,0次或更多')
PrintHelper.PrintSampleWithDescription('+', 'one or more,至少一次')
PrintHelper.PrintSampleWithDescription('?', 'zero or one,可能出现一次')
PrintHelper.PrintSampleWithDescription('{n,m}',
    'at least n and at most m,至少n次,最多m次')
PrintHelper.PrintSampleWithDescription('{n,}', 'at least n,至少n次')
PrintHelper.PrintSampleWithDescription('{n}', 'exactly n,正好n次')
PrintHelper.PrintTitle('Grouping,匹配分组')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r"(\\d{1,2})-(\\d{1,2})-(\\d{4})" )'
    )
PrintHelper.PrintCode(
    'm = pDate.search( "In response to your letter of 25-3-2015, \\ I decided to hire a hitman to get you." )'
    )
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( "Date {}; day {}; month {}; year {}"')
PrintHelper.PrintCode(
    '           .format( m.group(0), m.group(1), m.group(2), m.group(3) ) )')
import re
pDate = re.compile('(\\d{1,2})-(\\d{1,2})-(\\d{4})')
m = pDate.search(
    'In response to your letter of 25-3-2015, \\ I decided to hire a hitman to get you.'
    )
if m:
    print('Date {}; day {}; month {}; year {}'.format(m.group(0), m.group(1
        ), m.group(2), m.group(3)))
PrintHelper.PrintSubtitle('findall() and Groups')
PrintHelper.PrintHint('findall(@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r"(\\d{1,2})-(\\d{1,2})-(\\d{4})" )'
    )
PrintHelper.PrintCode(
    'datelist = pDate.findall( "In response to your letter of \\ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you." )'
    )
PrintHelper.PrintCode('for date in datelist:')
PrintHelper.PrintCode('    print( date )')
import re
pDate = re.compile('(\\d{1,2})-(\\d{1,2})-(\\d{4})')
datelist = pDate.findall(
    'In response to your letter of \\ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you.'
    )
for date in datelist:
    print(date)
PrintHelper.PrintSubtitle('Named Groups,组命名')
PrintHelper.PrintHint('(?P<@name>...)')
PrintHelper.PrintCode(
    'pDate = re.compile( r"(?P<day>\\d{1,2})-(?P<month>\\d{1,2})-(?P<year>\\d{4})")'
    )
PrintHelper.PrintCode(
    'm = pDate.search( "In response to your letter of 25-3-2015, \\ I curse you." )'
    )
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( "day is {}".format( m.group( \'day\') ) )')
PrintHelper.PrintCode(
    '    print( "month is {}".format( m.group( \'month\') ) )')
PrintHelper.PrintCode('    print( "year is {}".format( m.group( \'year\') ) )')
pDate = re.compile('(?P<day>\\d{1,2})-(?P<month>\\d{1,2})-(?P<year>\\d{4})')
m = pDate.search('In response to your letter of 25-3-2015, \\ I curse you.')
if m:
    print('day is {}'.format(m.group('day')))
    print('month is {}'.format(m.group('month')))
    print('year is {}'.format(m.group('year')))
PrintHelper.PrintSubtitle('Referring Within A Regular Expression,匹配内引用')
PrintHelper.PrintHint('\\n,如(\\S).*\\1指代任意非空格的字符,且重复两次,\\1指第一个匹配项')
PrintHelper.PrintTitle('Replacing,替换')
print('正则通常用以匹配内容,当然你也可以使用正则进行替换处理')
PrintHelper.PrintHint('re.sub(r@pattern,@replace,@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode(
    's = re.sub( r"([iy])se", "\\g<1>ze", "Whether you categorise, \\ emphasise, or analyse, you should use American spelling!" )'
    )
PrintHelper.PrintCode('print( s )')
import re
s = re.sub('([iy])se', '\\g<1>ze',
    'Whether you categorise, \\ emphasise, or analyse, you should use American spelling!'
    )
print(s)
print()

import PrintHelper
PrintHelper.PrintTitle('Regular Expressions With Python')
print('适用于数据挖掘,数据处理,抓取网页数据,大量的数据集合的模式化匹配')
print('正则表达式是字符串的模式化匹配,本章介绍如何使用正则表达式进行匹配')
PrintHelper.PrintSubtitle('The re Module')
PrintHelper.PrintHint('re,正则表达式模块')
PrintHelper.PrintHint('@pattern=re.compile(r@pattenString)','编译pattern')
PrintHelper.PrintHint('@pattern.findall(@stringForMatch)','使用pattern对象进行字符串匹配')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pAplus = re.compile( r\"a+\" )')
PrintHelper.PrintCode('lAplus = pAplus.findall( \"aardvark\" )')
PrintHelper.PrintCode('print( lAplus )')
import re
pAplus = re.compile( r"a+" )
lAplus = pAplus.findall( "aardvark" )
print( lAplus )
PrintHelper.PrintSubtitle('Shorthand,简写')
PrintHelper.PrintHint('re.findall(r@patternString,@stringForMatch)','直接使用patternString进行字符串匹配')
PrintHelper.PrintSubtitle('Match Objects')
print('当你需要更多的匹配信息的时候,re模块提供了一种称为MatchObjects的对象类型')
print('这种对象包含了更多的关于匹配的信息')
PrintHelper.PrintHint('@match=re.search(r@patternString,@stringForMatch)')
PrintHelper.PrintHint('@match.group()','匹配的内容')
PrintHelper.PrintHint('@match.start()','匹配的起始位置')
PrintHelper.PrintHint('@match.end()','匹配的结束位置')
PrintHelper.PrintCode('m = re.search( r\"a+\", \"Look out for the aardvark!\" )')
PrintHelper.PrintCode('print( \"{} is found at index {}\".format( m.group(), m.start() ) )')
m = re.search( r"a+", "Look out for the aardvark!" )
print( "{} is found at index {}".format( m.group(), m.start() ) )
PrintHelper.PrintSubtitle('Lists Of Matches',"多项匹配结果")
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('mlist = re.finditer( r\"a+\", \"Look out! A dangerous aardvark is on the loose!\" )')
PrintHelper.PrintCode('for m in mlist:')
PrintHelper.PrintCode('    print( \"{} is found at index {} and ends at index {}.\".format( m.group(), m.start(), m.end())) ')
import re
mlist = re.finditer( r"a+", "Look out! A dangerous aardvark is on the loose!" )
for m in mlist:
    print( "{} is found at index {} and ends at index {}.".format( m.group(), m.start(), m.end())) 
PrintHelper.PrintTitle('Writing Regular Expressions')
PrintHelper.PrintSubtitle('Regular Expressions With Square Brackets')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('slist = re.findall( r\"b[aeiou]ll\", \"Bill Gates and Uwe Boll \\ drank Red Bull at a football match in Campbell.\" )')
PrintHelper.PrintCode('print( slist )')
import re
slist = re.findall( r"b[aeiou]ll", "Bill Gates and Uwe Boll \ drank Red Bull at a football match in Campbell." )
print( slist )
PrintHelper.PrintSubtitle('Special Sequences,特殊序列标记')
PrintHelper.PrintSampleWithDescription('\\b','word boundary,单词边界')
PrintHelper.PrintSampleWithDescription('\\B','not a word boundary,非单次边界')
PrintHelper.PrintSampleWithDescription('\\d','digit,数字')
PrintHelper.PrintSampleWithDescription('\\D','not a digit,非数字')
PrintHelper.PrintSampleWithDescription('\\n','newline,换行')
PrintHelper.PrintSampleWithDescription('\\r','carriage return,回车')
PrintHelper.PrintSampleWithDescription('\\s','whitespace,空格')
PrintHelper.PrintSampleWithDescription('\\S','not a whitespace,非空格')
PrintHelper.PrintSampleWithDescription('\\t','tabulation,缩进')
PrintHelper.PrintSampleWithDescription('\\w','alphanumeric character,数字+字母')
PrintHelper.PrintSampleWithDescription('\\W','not an alphanumeric character,非数字和字母')
PrintHelper.PrintSampleWithDescription('\\/','forward slash,斜杠,左下斜杠')
PrintHelper.PrintSampleWithDescription('\\\\','backslash,反斜杠,右下反斜杠')
PrintHelper.PrintSampleWithDescription('\\\"','double quote,双眼号')
PrintHelper.PrintSampleWithDescription('\\\'','single quote,单眼号')
PrintHelper.PrintSampleWithDescription('-','start of a string,字符串头')
PrintHelper.PrintSampleWithDescription('$','end of a string,字符串尾')
PrintHelper.PrintSampleWithDescription('.','any character,任意字符')
PrintHelper.PrintSubtitle('Repetition,重复标记')
PrintHelper.PrintSampleWithDescription('*','zero or more,0次或更多')
PrintHelper.PrintSampleWithDescription('+','one or more,至少一次')
PrintHelper.PrintSampleWithDescription('?','zero or one,可能出现一次')
PrintHelper.PrintSampleWithDescription('{n,m}','at least n and at most m,至少n次,最多m次')
PrintHelper.PrintSampleWithDescription('{n,}','at least n,至少n次')
PrintHelper.PrintSampleWithDescription('{n}','exactly n,正好n次')
PrintHelper.PrintTitle('Grouping,匹配分组')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r\"(\\d{1,2})-(\\d{1,2})-(\\d{4})\" )')
PrintHelper.PrintCode('m = pDate.search( \"In response to your letter of 25-3-2015, \\ I decided to hire a hitman to get you.\" )')
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( \"Date {}; day {}; month {}; year {}\"')
PrintHelper.PrintCode('           .format( m.group(0), m.group(1), m.group(2), m.group(3) ) )')
import re
pDate = re.compile( r"(\d{1,2})-(\d{1,2})-(\d{4})" )
m = pDate.search( "In response to your letter of 25-3-2015, \ I decided to hire a hitman to get you." )
if m:
    print( "Date {}; day {}; month {}; year {}"
           .format( m.group(0), m.group(1), m.group(2), m.group(3) ) )
PrintHelper.PrintSubtitle('findall() and Groups')
PrintHelper.PrintHint('findall(@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('pDate = re.compile( r\"(\\d{1,2})-(\\d{1,2})-(\\d{4})\" )')
PrintHelper.PrintCode('datelist = pDate.findall( \"In response to your letter of \\ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you.\" )')
PrintHelper.PrintCode('for date in datelist:')
PrintHelper.PrintCode('    print( date )')
import re
pDate = re.compile( r"(\d{1,2})-(\d{1,2})-(\d{4})" )
datelist = pDate.findall( "In response to your letter of \ 25-3-2015, on 27-3-2015 I decided to hire a hitman to get you." )
for date in datelist:
    print( date )
PrintHelper.PrintSubtitle('Named Groups,组命名')
PrintHelper.PrintHint('(?P<@name>...)')
PrintHelper.PrintCode('pDate = re.compile( r\"(?P<day>\\d{1,2})-(?P<month>\\d{1,2})-(?P<year>\\d{4})\")')
PrintHelper.PrintCode('m = pDate.search( \"In response to your letter of 25-3-2015, \\ I curse you.\" )')
PrintHelper.PrintCode('if m:')
PrintHelper.PrintCode('    print( \"day is {}\".format( m.group( \'day\') ) )')
PrintHelper.PrintCode('    print( \"month is {}\".format( m.group( \'month\') ) )')
PrintHelper.PrintCode('    print( \"year is {}\".format( m.group( \'year\') ) )')
pDate = re.compile( r"(?P<day>\d{1,2})-(?P<month>\d{1,2})-(?P<year>\d{4})")
m = pDate.search( "In response to your letter of 25-3-2015, \ I curse you." )
if m:
    print( "day is {}".format( m.group( 'day') ) )
    print( "month is {}".format( m.group( 'month') ) )
    print( "year is {}".format( m.group( 'year') ) )
PrintHelper.PrintSubtitle('Referring Within A Regular Expression,匹配内引用')
PrintHelper.PrintHint('\\n,如(\\S).*\\1指代任意非空格的字符,且重复两次,\\1指第一个匹配项')
PrintHelper.PrintTitle('Replacing,替换')
print('正则通常用以匹配内容,当然你也可以使用正则进行替换处理')
PrintHelper.PrintHint('re.sub(r@pattern,@replace,@stringForMatch)')
PrintHelper.PrintCode('import re')
PrintHelper.PrintCode('s = re.sub( r\"([iy])se\", \"\\g<1>ze\", \"Whether you categorise, \\ emphasise, or analyse, you should use American spelling!\" )')
PrintHelper.PrintCode('print( s )')
import re
s = re.sub( r"([iy])se", "\g<1>ze", "Whether you categorise, \ emphasise, or analyse, you should use American spelling!" )
print( s )
print()

