Regex Capturing Group Within Non-capturing Group
Solution 1:
Your question is phrased strictly about regex, but if you're willing to use a recursive descent parser (e.g., pyparsing
), many things that require expertise in regex, become very simple.
E.g., here what you're asking becomes
from pyparsing import *
p = Suppress(Literal('import')) + commaSeparatedList
>>> p.parseString('import pandas, os, sys').asList()
['pandas', 'os', 'sys']
>>> p.parseString('import pandas, os').asList()
['pandas', 'os']
It might be a matter of personal taste, but to me,
Suppress(Literal('import')) + commaSeparatedList
is also more intuitive than a regex.
Solution 2:
A repeated capturing group will only capture the last iteration. This is why you need to restructure your regex to work with re.findall
.
\s*
(?:
(?:^from\s+
( # Base (from (base) import ...)
(?:[a-zA-Z_][a-zA-Z_0-9]* # Variable name
(?:\.[a-zA-Z_][a-zA-Z_0-9]*)* # Attribute (.attr)
)
)\s+import\s+
)
|
(?:^import\s|,)\s*
)
( # Name of imported module (import (this))
(?:[a-zA-Z_][a-zA-Z_0-9]* # Variable name
(?:\.[a-zA-Z_][a-zA-Z_0-9]*)* # Attribute (.attr)
)
)
(?:
\s+as\s+
( # Variable module is imported into (import foo as bar)
(?:[a-zA-Z_][a-zA-Z_0-9]* # Variable name
(?:\.[a-zA-Z_][a-zA-Z_0-9]*)* # Attribute (.attr)
)
)
)?
\s*
(?=,|$) # Ensure there is another thing being imported or it is the end of string
Capture group 0 will be the Base
, capture group 1 will be (What you're after) the name of the imported module, and capture group 2 will be the variable the module is in (from (group 0) import (group 1) as (group 2)
)
import re
regex = r"\s*(?:(?:^from\s+((?:[a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*))\s+import\s+)|(?:^import\s|,)\s*)((?:[a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*))(?:\s+as\s+((?:[a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*)))?\s*(?=,|$)"print(re.findall(regex, "import pandas, os, sys"))
[('', 'pandas', ''), ('', 'os', ''), ('', 'sys', '')]
You can remove the other two capturing groups if you don't care for them.
Solution 3:
You can use your import\s+(?:([a-zA-Z0-9=]+),*\s*)*
regex (I just fixed the 0-9
range to match any digit and included =
to the end) and access the Group 1 capture stack using PyPi regex module:
>>>import regex>>>s = 'import pandas, os, sys'>>>rx = regex.compile(r'^import\s+(?:([a-zA-Z0-9=]+),*\s*)*$')>>>print([x.captures(1) for x in rx.finditer(s)])
[['pandas', 'os', 'sys']]
Post a Comment for "Regex Capturing Group Within Non-capturing Group"