Using pyparsing to match against a specific ending of a string
Question
How can I build a python pyparsing structure that matches against a particular string ending. For example, suppose I want to find all words that end in 'ing'. The following does NOT work:
ing_ending = Word(alphas) + Literal("ing") + White();
ing_ending.parseString("jumping")
This would be pretty easy to do in python and regex but gives me trouble in pyparsing.
Solution
Just use regex
>>> import pyparsing
>>> ing_ending = pyparsing.Regex(r'(?i)\b[a-z]+ing\b')
>>> ing_ending.parseString("jumping")
(['jumping'], {})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow