无法将列表中的最后一项与正则表达式匹配

时间:2018-04-09 21:23:31

标签: regex python-3.x

我需要使用Python中的正则表达式检测此键/值对:

key_title=[a3;d2;g5;a5]

值是由“;”

分隔的元素列表

...但它不起作用,因为< values>中的最后一个元素不以“;”结尾。

我有这个python正则表达式:

(?P<key_values_pair>^(?P<text>\w+)\s*=\s*\[(?P<values>[a-zAZ]\d;)+\])

和这个匹配的数据

title=[a3;d2;g5;a5] #comment
# other comment
some_key=[j7;k9]

2 个答案:

答案 0 :(得分:1)

这似乎有效,我已经略微简化并使分号可选:

(?P<key>\w+)=\[(?P<values>(\w+\d;?)+)\]

regex101 demo

答案 1 :(得分:0)

为什么不简单地将分号设为可选,如下所示:

IF OBJECT_ID('vTotalWebSalesPerCustomer', 'v') IS NOT NULL
    DROP VIEW vTotalWebSalesPerCustomer;
GO

CREATE VIEW vTotalWebSalesPerCustomer AS
    SELECT 
        x.CustomerID,
        LTRIM(RTRIM(CONCAT(p.Title, ' ', p.LastName, ', ', p.FirstName, ' ', p.Suffix))) AS NomClient,
        x.TotalDue
    FROM (
        SELECT 
            c.CustomerID AS CustomerID,
            SUM(soh.TotalDue) AS TotalDue
        FROM [Sales].[Customer] AS c
        LEFT JOIN [Sales].[SalesOrderHeader] AS soh ON soh.CustomerID = c.CustomerID
        WHERE YEAR(soh.OrderDate) = 2014 AND DATEPART(quarter, soh.OrderDate) = 1 AND [OnlineOrderFlag] = 1
        GROUP BY c.CustomerID
    )x LEFT JOIN [Person].[Person] AS p ON x.CustomerID = p.BusinessEntityID
GO