erikrose / parsimonious

The fastest pure-Python PEG parser I can muster
MIT License
1.81k stars 127 forks source link

How to search for substring with grammar? #237

Open nurdann opened 1 year ago

nurdann commented 1 year ago

Can I use grammar to search for its occurrence in a text instead of matching the entire string?

e.g with following grammar

log = "(" userid "&" lat ")"
userid = ~"user_?id=[0-9a-zA-Z]+"
lat = "lat" delimeter float
delimeter = "=" / ":"

float = digits ("." digits)?
digits = ~"[0-9]+"

I can match

(user_id=1000&lat=23.3)

Can I search for substring? e.g.

prefix (user_id=1000&lat=23.3)

And more complex: what if I want to search for key-value pairs, e.g. user_id, lat inside paranthesis? so I'd want to match the following strings as well,

(... user_id=1000 .... lat=23.3 ...)

Would unwanted characters be part of the grammar as well?