1

I have a string like this

SELECT [Orders$].[Category] AS [Category],&#13,&#10,  [Orders$].[City] AS [City],&#13,&#10,  [Orders$].[Country] AS [Country],&#13,&#10,  [Orders$].[Customer ID] AS [Customer ID],&#13,&#10,  [Orders$].[Customer Name] AS [Customer Name],&#13,&#10,  [Orders$].[Discount] AS [Discount],&#13,&#10,  [Orders$].[Profit] AS [Profit],&#13,&#10,  [Orders$].[Quantity] AS [Quantity],&#13,&#10,  [Orders$].[Region] AS [Region],&#13,&#10,  [Orders$].[State] AS [State],&#13,&#10,  [People$].[Person] AS [Person],&#13,&#10,  [People$].[Region] AS [Region (People)]&#13,&#10,FROM [Orders$]&#13,&#10,  INNER JOIN [People$] ON [Orders$].[Region] = [People$].[Region]

I want to get only Category and city dynamically without hardcoding the word . What kind of pattern should i use ?? So that i will store those two values in an array which is looped in downstream program .

I tried splitting the text

colName = re.split("\W+", result)

['SELECT',
 'Orders',
 'Category',
 'AS',
 'Category',
 '13',
 '10',
 'Orders',
 'City',
 'AS',
 'City',
 '13',
 '10',

it gave me the whole string , now do not know how to proceed . Can someone help ??

Thanks

1
  • So when using findall and you get category and city, how do you know which is which ? Commented Jul 16, 2020 at 21:25

2 Answers 2

1

Don't use split, use re.findall().

matches = re.findall(r'\bAS\s+\[(.+?)\]', yourString)

The words you want are in group(1) of each match in matches.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much for the quick response , it worked :)
I got only 9 colums but I have 12 columns I have edited in the question the actual select query . Can you please have a look . Sorry to bother ...Thanks What I did get was Customer ID , Customer Name and Region (People)
Some of your column names have spaces, my regexp assumed they were just alphanumeric
Thank you very much it worked , you are a great genius for helping others :) .
0

Not sure if I understand your question correctly, seems you can simply continue with:

>>> category = colName[2]
>>> city = colName[8]

You can print to check:

>>> print(category, city)
Category City

2 Comments

Hard-coding specific indexes doesn't seem very robust.
You're absolutely right @Barmar, I just thought it can be a cheap solution for the example given in the question, which seems to be a fixed format query. The regex in your answer is surely more reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.