r1chardj0n3s / parse

Parse strings using a specification based on the Python format() syntax.
http://pypi.python.org/pypi/parse
MIT License
1.71k stars 101 forks source link

Search for a certain pattern error at the end of a string #182

Closed popu2do closed 7 months ago

popu2do commented 7 months ago

parse: 1.20.1 python: 3.9.13

>>from parse import search
>>rule = "auto expo center 20px mean = {AEB}"
>>line = 'xxx auto expo center 20px mean = [6798, 5486]'
>>aeb = search(rule, line)
>>print(aeb)
>>><Result () {'AEB': '['}>

As you can see,AEB doesn't seem to be handled correctly, I think that this is a bug? my interim solution is end of line append tag to avoid bug,

>>rule = rule + "# endofline #"
>>line = line + "# endofline #"

Is there a better way?

wimglenn commented 7 months ago

If you want to match the entire string you can use parse instead of search, the prefix can be discarded:

>>> result = parse(
...     format="{x} auto expo center 20px mean = {AEB}",
...     string="xxx auto expo center 20px mean = [6798, 5486]",
... )
>>> result.named["AEB"]
'[6798, 5486]'

The behavior of search does not seem like a bug to me. This function searches a string for the first occurrence of the format, and without any other constraints, the non-greedy result you see capturing a [ is indeed the first occurrence.