简体   繁体   中英

Write a regex to find sentence inside the parentheses

I have to use regex inside C# program. Input string:

WITH Sum_OrderQuantity_CTE AS ( SELECT ProductKey, EnglishMonthName, SUM(OrderQuantity) AS TotalOrdersByMonth FROM [dbo].[FactInte.netSales] fs INNER JOIN [dbo].[DimDate] dd ON dd.DateKey = fs.OrderDateKey GROUP BY ProductKey, EnglishMonthName ) SELECT ProductKey, AVG(TotalOrdersByMonth) AS 'Average Total Orders By Month' FROM Sum_OrderQuantity_CTE GROUP BY ProductKey ORDER BY ProductKey

I want to find strings which come after AS and is present in the parentheses. There can be multiple AS in the string. I want to find all those sentence present in the parentheses and come after AS.

I have tried some regex but failed to find correct matches:

AS\s{1,}\((.*?)\)\s{1,}(?=SELECT|UPDATE |INSERT|DELETE|AS\s{1,}\()
AS \((.*)\)
(?=AS\s{1,}\((.*)\))

This regex is the one you're looking for:

(?<=AS \()([^\(\)]*(\([^\(\)]*\))?[^\(\)]*)*(?=\))

Link: https://regex101.com/r/m9jLcs/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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