Skip to content

Add groups argument to Regex filter #40

Open
@todofixthis

Description

@todofixthis

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions