Home > Enterprise >  Write a regex to find sentence inside the parentheses
Write a regex to find sentence inside the parentheses

Time:10-06

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

WITH Sum_OrderQuantity_CTE AS (SELECT ProductKey, EnglishMonthName, SUM(OrderQuantity) AS TotalOrdersByMonth FROM [dbo].[FactInternetSales] 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,}\((.*)\))

CodePudding user response:

This regex is the one you're looking for:

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

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

  • Related