[New-bugs-announce] [issue36158] Regex search behaves differently in list comprehension
Matthew Drago
report at bugs.python.org
Fri Mar 1 12:21:57 EST 2019
New submission from Matthew Drago <mattdrago9 at gmail.com>:
Say for example i want to apply a regex on a list of strings.
Using list comprehension as such results in the group method not being found.
```
name_regex = compile(r'\[\"([a-zA-Z\s]*)\"{1}')
named_entities = [name_regex.match(entity.trigger).group(1) for entity in entities[0]]
```
This unexpected behavior can also be observed when implementing this using a map.
```
list(map(lambda x: name_regex.search(x.trigger).group(), entities[0]))
```
However using the traditional for loop implementation the group method is resolved.
```
named_entities = []
for entity in entities[0]:
named_entities.append(name_regex.match(entity.trigger).group(1))
```
----------
components: Regular Expressions
messages: 336936
nosy: Matthew Drago, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: Regex search behaves differently in list comprehension
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36158>
_______________________________________
More information about the New-bugs-announce
mailing list