Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add groups argument to Regex filter #40

Open
todofixthis opened this issue Mar 19, 2018 · 0 comments
Open

Add groups argument to Regex filter #40

todofixthis opened this issue Mar 19, 2018 · 0 comments

Comments

@todofixthis
Copy link
Contributor

todofixthis commented Mar 19, 2018

Often when I use the Regex filter, I'm not interested in capturing the matching groups as a list; I just want to verify that the incoming string matches the regular expression, and then continue applying filters to the original string.

Add a groups argument to Regex that can be set to any of the following:

  • True (default, for bc): Filter returns all of the matching groups as a list (this is the current behaviour).
  • False: Filter returns the input string.
  • Iterable[int]: Filter returns a list containing only the specified groups.
  • int (maybe): Filter returns just that group.

That last one feels a bit off, both semantically and functionally. But, it could be useful, so I'll float the idea anyway.

Examples:

>>> value = '42-86-99'

# Default behaviour
>>> Regex('\d+').apply(value)
['42', '86', '99']
>>> Regex('\d+', groups=True).apply(value)
['42', '86', '99']

>>> Regex('\d+', groups=False).apply(value)
'42-86-99'

>>> Regex('(\d)(\d)', groups=(0, 1)).apply(value)
['42', '4']

# Maybe
>>> Regex('(\d)(\d)', groups=1).apply(value)
'4'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant