引用1楼akirya的回复:对于正则来讲基本上是不可能的任务。
还是搞语法分析来的快一些。
boost::regex应该是可以实现的,我在官网上找到如下内容,但就是不会写。
RecursiveExpressions
(?N)(?-N)(?+N)(?R)(?0)
(?R)and(?0)recursetothestartoftheentirepattern.
(?N)executessub-expressionNrecursively,forexample(?2)willrecursetosub-expression2.
(?-N)and(?+N)arerelativerecursions,soforexample(?-1)recursestothelastsub-expressiontobedeclared,and(?+1)recursestothenextsub-expressiontobedeclared.
ConditionalExpressions
(?(condition)yes-pattern|no-pattern)attemptstomatchyes-patterniftheconditionistrue,otherwiseattemptstomatchno-pattern.
(?(condition)yes-pattern)attemptstomatchyes-patterniftheconditionistrue,otherwisefails.
conditionmaybeeither:aforwardlookaheadassert,theindexofamarkedsub-expression(theconditionbecomestrueifthesub-expressionhasbeenmatched),oranindexofarecursion(theconditionbecometrueifweareexecutingdirectlyinsidethespecifiedrecursion).
Hereisasummaryofthepossiblepredicates:
(?(?=assert)yes-pattern|no-pattern)Executesyes-patterniftheforwardlook-aheadassertmatches,otherwiseexecutesno-pattern.
(?(?!assert)yes-pattern|no-pattern)Executesyes-patterniftheforwardlook-aheadassertdoesnotmatch,otherwiseexecutesno-pattern.
(?(R)yes-pattern|no-pattern)Executesyes-patternifweareexecutinginsidearecursion,otherwiseexecutesno-pattern.
(?(RN)yes-pattern|no-pattern)Executesyes-patternifweareexecutinginsidearecursiontosub-expressionN,otherwiseexecutesno-pattern.
(?(DEFINE)never-exectuted-pattern)Definesablockofcodethatisneverexecutedandmatchesnocharacters:thisisusuallyusedtodefineoneormorenamedsub-expressionswhicharereferedtofromelsewhereinthepattern.
不知道boost的regex如何写。
|