Reputation: 33
I have a string like this
SELECT [Orders$].[Category] AS [Category],
,
, [Orders$].[City] AS [City],
,
, [Orders$].[Country] AS [Country],
,
, [Orders$].[Customer ID] AS [Customer ID],
,
, [Orders$].[Customer Name] AS [Customer Name],
,
, [Orders$].[Discount] AS [Discount],
,
, [Orders$].[Profit] AS [Profit],
,
, [Orders$].[Quantity] AS [Quantity],
,
, [Orders$].[Region] AS [Region],
,
, [Orders$].[State] AS [State],
,
, [People$].[Person] AS [Person],
,
, [People$].[Region] AS [Region (People)]
,
,FROM [Orders$]
,
, 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
Upvotes: 1
Views: 205
Reputation: 780724
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
.
Upvotes: 1
Reputation: 1131
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
Upvotes: 0