Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to parse import statements in python

Tags:

can someone help me writing single regex to get module(s) from python source line?

from abc.lmn import pqr
from abc.lmn import pqr as xyz
import abc
import abc as xyz

it has 3 sub parts in it

[from(\s)<module>(\s)] --> get module if this part exist
import(\s)<module>     --> get module
[(\s)as(\s)<alias>]    --> ignore if this part exist

something like this

:?[from(\s)<module>(\s)]import(\s)<module>:?[(\s)as(\s)<alias>]