[英]Regex in python splitting strings
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谢谢
Don't use split, use re.findall()
.不要使用拆分,使用re.findall()
。
matches = re.findall(r'\bAS\s+\[(.+?)\]', yourString)
The words you want are in group(1)
of each match in matches
.您想要的单词在 match 中每个matches
项的group(1)
中。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.