I'm using the parse
library and ran into surprising (to me) functionality: it does not match empty strings:
>>> from parse import parse
>>> parse('hi "{}"', 'hi "everybody"')
<Result ('everybody',) {}>
>>> parse('hi "{}"', 'hi ""')
>>>
Is there a way, using parse
, to get it to match any string between ""
in the same way that re
does:
>>> from re import match
>>> match('hi "(.*)"', 'hi "everybody"').groups()
('everybody',)
>>> match('hi "(.*)"', 'hi ""').groups()
('',)
Use a custom type conversion:
from parse import parse
def zero_or_more_string(text):
return text
zero_or_more_string.pattern = r".*"
parse('hi "{:z}"', 'hi ""', { "z": zero_or_more_string })
and you'll get this:
<Result ('',) {}>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With