# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<!\d)(?:\d{12}|\d{8})\.?(?:[0-9xX])(?!\d)"
test_str = ("申请号\n"
"002623110\n"
"022879803\n"
"2004100184779\n"
"2004100666004\n"
"2005101110245\n"
"2006300002925\n"
"2006200007177\n"
"2007100385983\n"
"2008101335952\n"
"2008101613271\n"
"2011100997897\n"
"2008201400425\n"
"2009101423229\n"
"2009101423233\n"
"2009101423248\n"
"2009101439477\n"
"2009101572384\n"
"2009101572399\n"
"2009101576224\n"
"2009302031381\n"
"2009101610776\n"
"2009201695623\n"
"2011200054060\n"
"2011200115844\n"
"2011200117483\n"
"2011200119760\n"
"2011200173252\n"
"2009202675059\n"
"2009202695692\n"
"2010101657760\n"
"2010101870386\n"
"2010102394457\n"
"2010102394599\n"
"2010202748438\n"
"2010202748442\n"
"2010202748461\n"
"2010305347887\n"
"2010305348019\n"
"201120005687X\n"
"2011200119900\n"
"2011200173290\n"
"2010105277084\n"
"2010105566205\n"
"2011200349620\n"
"2011100914818\n"
"2011100914945\n"
"2011101671599\n"
"2011202170507\n"
"2011303345684\n"
"2011103392345\n"
"2011204598533\n"
"2011204598548\n"
"201120459882X\n"
"2011204728081\n"
"2011204728185\n"
"2011103986574\n"
"201220034593X\n"
"2012200534787\n"
"2012101185754\n"
"2012201875401\n"
"2012101522194\n"
"2012101616153\n"
"201210181171X\n"
"201210182746X\n"
"2012302578183\n"
"2012102717768\n"
"2012204355254\n"
"2012103245533\n"
"201210339474X\n"
"2012103530528\n"
"2012103625443\n"
"2012103785724\n"
"2012205133542\n"
"2012306400917\n"
"2013100109283\n"
"2013100347741\n"
"2013200572675\n"
"2013100686967\n"
"2013100887627\n"
"201310101959X\n"
"2013204392703\n"
"2013103116132\n"
"2013204821094\n"
"2013103430914\n"
"2013205073300\n"
"201320567066X\n"
"2013205672542\n"
"2013104198233\n"
"2013205697728\n"
"201310438266X\n"
"2013206047657\n"
"2013206784136\n"
"2013106979478\n"
"2013107401728\n"
"2013208786475\n"
"2014100017892\n"
"2014203613224\n"
"2014103129866\n"
"2014103129917\n"
"2014203668496\n"
"2014103211663\n"
"2014103512302\n"
"2014104555767\n"
"2014205304935\n"
"2014104903341\n"
"2014104919602\n"
"2014205488892\n"
"201420580619X\n"
"2014106419682\n"
"2014206836544\n"
"2014107834019\n"
"2014305467389\n"
"2015203286585\n"
"2015203324750\n"
"201510275816X\n"
"2015103420381\n"
"2015103500672\n"
"2015103696669\n"
"2015204541209\n"
"2015204664745\n"
"2015103923075\n"
"201510467622X\n"
"2015205745010\n"
"2015205769284\n"
"2015207387890\n"
"2015207443706\n"
"2015106236775\n"
"201510624713X\n"
"2015207542082\n"
"2015207551452\n"
"2015207556085\n"
"2015110314940\n"
"2015211349209\n"
"2015211360104\n"
"2015211381702\n"
"2016100727763\n"
"201610092328X\n"
"2016201269151\n"
"2016100951186\n"
"201620131265X\n"
"2016101122274\n"
"2016201537615\n"
"2016202566259\n"
"2016102311131\n"
"2016205198507\n"
"2016105865929\n"
"2016207787667\n"
"2016207928858\n"
"2016207951375\n"
"2016208270639\n"
"2016208319490\n"
"2016106880470\n"
"2016106880555\n"
"2016209033823\n"
"201621066422X\n"
"2016108656802\n"
"2016210939684\n"
"2016305258880\n"
"2016305258895\n"
"2017100585076\n"
"2017100586609\n"
"201720188818X\n"
"2017101518115\n"
"2017202603036\n"
"2017204977946\n"
"2017205042156\n"
"2017103350053\n"
"2017205395444\n"
"2017206736253\n"
"2017302335447\n"
"2017104425076\n"
"2017106763096\n"
"201721030029X\n"
"2017210381102\n"
"2017108045288\n"
"2017108474438\n"
"2017212101341\n"
"2017108741867\n"
"2017108759339\n"
"2017212638027\n"
"201710920118X\n"
"201710937864X\n"
"201710938025X\n"
"2017212918579\n"
"2017212918668\n"
"2017212919919\n"
"2017212920244\n"
"2018305555227\n"
"2017109935766\n"
"201710994526X\n"
"2017109953730\n"
"201710995375X\n"
"2017213675894\n"
"2017213676191\n"
"2017213684060\n"
"2017213691670\n"
"2017214075375\n"
"2017215966451\n"
"201730622561X\n"
"2017306229748\n"
"2017306232613\n"
"2017306232628\n"
"2017217386944\n"
"2018201916983\n"
"2018201917914\n"
"2018202592290\n"
"201810191260X\n"
"2018203227754\n"
"2018104322962\n"
"2018206790888\n"
"2018104373610\n"
"2018206885731\n"
"2018207863744\n"
"201820786380X\n"
"2018207884967\n"
"2018105251184\n"
"201820872835X\n"
"2018209012008\n"
"2018106635891\n"
"2018209871622\n"
"2018303397159\n"
"2018303401597\n"
"2018108107645\n"
"2018211712732\n"
"2018108434473\n"
"2018212145039\n"
"201810884066X\n"
"2018212612587\n"
"2018109232869\n"
"2018213117869\n"
"2018213118293\n"
"2018213129669\n"
"2018109730845\n"
"2018213745478\n"
"2018111137608\n"
"2018111199286\n"
"2018215601739\n"
"2018216540022\n"
"2018217340269\n"
"2018305943345\n"
"2018114173714\n"
"201821980006X\n"
"201811454311X\n"
"201822010483X\n"
"2018114822527\n"
"201822097698X\n"
"2018220978171\n"
"2019100186094\n"
"201920036562X\n"
"2019200056202\n"
"2019204835323\n"
"2019103446604\n"
"2019205950070\n"
"2019102993026\n"
"2019205043321\n"
"2019102887031\n"
"2019204926623\n"
"2019204495410\n"
"2019209514723\n"
"2019210406537\n\n\n\n\n"
"2019301669399\n"
"2019301669257\n"
"2019105752159\n"
"2019210048170\n"
"2019106651386\n\n\n\n\n"
"2019212083094\n\n\n\n\n\n\n\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html