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)
if match:
# Get the first matched group; group(0) matches entire string
return match.group(1)
if match:
# Get the last matched group or entire string if there are no groups
return match.group(len(match.groups))
>>> 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'
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))
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 beancount+...@googlegroups.com.
To post to this group, send email to bean...@googlegroups.com.
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.
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,ShreedharPatches welcome.
----
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 beancount+...@googlegroups.com.
To post to this group, send email to bean...@googlegroups.com.
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.
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 beancount+...@googlegroups.com.
To post to this group, send email to bean...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/CAK21%2BhPBsqHP_9DSC8jnSTDKMiEkhtYuJcmG9_KaStfqBxSpVw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/CAAY9sD-MjrNtCgnkeS9RS2aT0XLUEpRXUt0_ej6g2W3TrdoFzg%40mail.gmail.com.
Thanks Shreedhar.I'll merge your patch in the next few weeks (I have some time off coming up).