Extract patterns in order (Python)

I need to implement the following:

Input: type1("s df")  type2("df g")  type3("ds ff   d")  type4("fdgfg")

I need to extract the patterns type1(.*), type2(.*), type3(.*), and type4(.*), in the same order as they are found in the input and store them in an array. ( I have exactly 4 types)

I read about re.split, re.match, re.search, etc. But, I don't think I can use any of them to achieve my goal.
Please tell me how I must approach this.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm not entirely clear on your input. If these "patterns" all occur in the same search string, then you should be able to use the following. You would inspect the group member of the returned object from match(). Group 1 would be "type1", group 2 would be "type 2", etc.
m = re.match(r"type1\(([^)]*).*?type2\(([^)]*).*?type3\(([^)]*).*?type4\(([^)]*)", "type1(\"s df\")  type2(\"df g\")  type3(\"ds ff   d\")  type4(\"fdgfg\")")

Open in new window

dshrenikAuthor Commented:
I don't think I have made my requirement clear.

The output for the example input you considered should be: (Lets use single quotes to avoid escape sequences)
type1('s df')
type2('df g')
type3('ds ff   d')

The input can have any of these 4 types appear in any order, any number of times.
For example if the input is " type1('ds f') type3('dsf f') type1('sdf') type1('dsf'), then the output must be collected in an array in this order:
type1('ds f')
type3('dsf f')

So what I need is to collect each type (as a whole) in the order they appear.

Thanks for your efforts! Hope what I want to get isn't very difficult to implement.
Is this more to the desired result?
re.findall(r"(type\d+\([^)]*\))", "type1('ds f') type3('dsf f') type1('sdf') type1('dsf')")

Open in new window

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

dshrenikAuthor Commented:
Thats great!
But it wouldn't work always for me, since not all types are name as type1, type2, etc.
I did that only for convenience. Sorry for making myself clear.

The actual types are:
exa('dsfdsf'), almost('sdfdsf'), any('sdf sdf')
Please leave me know if it is possible to customize this. Sorry for the trouble!  
It's cool. Since regex is all about matching patterns, we need to come up with some pattern that will be common to all your strings. Is it safe to assume that a pseudo-pattern would be:

    a string of alphanumerics, followed by an opening parentheses, followed by some string (possibly quoted), followed by a closing parentheses

dshrenikAuthor Commented:
Sure. I guess that should work.
re.findall(r"(\w+\d+\([^)]*\))", "type1('ds f') type3('dsf f') type1('sdf') type1('dsf')")

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dshrenikAuthor Commented:
Thanks. That works great. Needed just 1 modification - we need to remove \d+.

If possible, can you address this question?
>>  we need to remove \d+

True enough. I'm up past my bedtime and I think my brain is slowing down on me, trying to tell me to go to bed  ;)

Glad it worked for you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.