简体   繁体   English

python 中的正则表达式拆分字符串

[英]Regex in python splitting strings

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谢谢

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM