Getting pyparsing to backtrack

John Nagle nagle at animats.com
Tue Jul 6 00:45:07 EDT 2010


On 7/5/2010 3:19 PM, John Nagle wrote:
>   I'm working on street address parsing again, and I'm trying to deal
> with some of the harder cases.

The approach below works for the cases given.  The "Or" operator ("^") 
supports backtracking, but "Optional()" apparently does not.


direction = Combine(MatchFirst(map(CaselessKeyword, directionals)) +
     Optional(".").suppress())
	
streetNameOnly = Combine(OneOrMore(Word(alphanums)), adjacent=False,
     joinString=" ").setResultsName("streetname")

streetNameParser =
     ((direction.setResultsName("predirectional") + streetNameOnly)
     ^ streetNameOnly)



					John Nagle



More information about the Python-list mailing list