Hi,
Just noticed that the implementation of the grep function in bean-query
doesn't make sense to me from the description:
class Grep(query_compile.EvalFunction):
"Match a group against a string and return only the matched portion."
__intypes__ = [str, str]
def __init__(self, operands):
super().__init__(operands, str)
def __call__(self, context):
args = self.eval_args(context)
match = re.search(args[0], args[1])
if match:
return match.group(0)
According to the description I think it should do:
if match:
# Get the first matched group; group(0) matches entire string
return match.group(1)
or even:
if match:
# Get the last matched group or entire string if there are no
groups
return match.group(len(match.groups))
Reference: https://docs.python.org/3/library/re.html#match-objects
If it is implemented as intended, I suppose it would be nice to have an
overloaded grep() function that takes a 3rd parameter of type int, for the
group id. I can send a patch for that if you prefer that, although I think
the second implementation should work for both styles:
>>> import re
>>> m = re.search('a (b) c', 'asda b c')
>>> m.group(len(m.groups()))
'b'
>>> m = re.search('a b c', 'asda b c')
>>> m.group(len(m.groups()))
'a b c'
Thanks,
Shreedhar
--
You received this message because you are subscribed to the Google Groups
"Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/beancount/5b9484fc-1bd0-4e8c-81b4-f6caa2877cea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.