On 30.06.2011 15:09, Mathias Panzenböck wrote:
I would rather add a grep method to regular expression objects:
def grep(self,sequence): for s in sequence: if self.search(s): yield s
And a global function to the re module:
def grep(regex,sequence,flags=0): return re.compile(regex,flags).grep(sequence)
Then you could do:
Yes, it would be more to type but it would be more general.
No you couldn't, since this would return <generator ...> -- an explicit list() would be required.
OT: I think it would be good if regular expressions would be callable with this method:
def __call__(self,s): return self.search(s) is not None
Then one could use it in a filter expression:
What's wrong with
? And for non-interactive use, you almost always have a compiled regex object already, so that this becomes
which is not sufficiently harder than
to justify a new regex method. (Also, people won't be able to remember if grep() uses match() or search().)