首页 > 解决方案 > Match package name(s) using regex

问题描述

I have Python import statements that I want to match using regex.

I have these four types of strings,

  1. import collections as c

I want to match just the last word 'c'. => import collections as c

  1. from datetime import date as dt

I want to match just the last word 'dt'. => from datetime import date as dt

  1. from sys import stdout, stderr, stdin

I want to match everything after import. => from sys import stdout, stderr, stdin

  1. import re

I want to match just the last word 're' => import re

Currently I am using inbuit way to split by "import" and get the string after that which seems inefficient.

I tried doing this,

(?<=import ).*

but it just matches case 3 and case 4, it doesn't match case 1 and case 2.

How do I do this using regex?

标签: pythonregex

解决方案


This works for your four cases, and I believe it should be mostly general:

re.search('import( \S* as)? (.*)$',your_string)[2]

briefly, you search for all the words that come at the end (which is basically what you always want) after 'import', but you want XXX as to be excluded in case these follow the word import so you put that as an optional match (in parentheses). Finally, you select your second matching item. Hope this is clear


推荐阅读