2

I want to match specifically the comma "," after the two groups ( AS) and (.*?). I have a positive lookbehind that skips the AS but I cant get the grouping to skip the wildcard lazy group.

Regex:

(?<= AS)(.*?)(,)

Sample text

    SELECT     LEFT(CustomerCode, 5) AS SMSiteCode, SUBSTRING(CustomerCode, 6, LEN(CustomerCode) - 5) AS SMCustCode, SUBSTRING(AgreeNo, 6, LEN(AgreeNo) - 5) 
                  AS SMAgreeNo, CAST(SeqNo AS int) AS SeqNo, SUBSTRING(TrxDate, 7, 2) + SUBSTRING(TrxDate, 4, 2) + SUBSTRING(TrxDate, 1, 2) AS TrxDate, TrxTime, 
                  CAST(Charge AS bit) AS Charge, CASE WHEN LEN(AnalysisCode) > 5 THEN SUBSTRING(AnalysisCode, 6, LEN(AnalysisCode) - 5) 
                  ELSE AnalysisCode END AS AnalysisCode, CAST(ISNULL(Description, N'') AS nvarchar(100)) AS Description, CAST(TaxAmt AS money) AS TaxAmt, 
                  CAST(TotAmt AS money) AS TotAmt, CAST(Match AS bigint) AS Match, CAST(Confirmed AS bit) AS Confirmed, CAST(Balance AS money) AS Balance, 
                  CAST(QtyBal AS money) AS QtyBal, CAST(ISNULL(Drawer, N'') AS nvarchar(50)) AS Drawer, SUBSTRING(DateBanked, 7, 2) + SUBSTRING(DateBanked, 4, 2) 
                  + SUBSTRING(DateBanked, 1, 2) AS DateBanked, CAST(ISNULL(BankBranch, N'') AS nvarchar(50)) AS BankBranch, CAST(Qty AS float) AS Qty, CAST(ISNULL(Narration, 
                  N'') AS nvarchar(100)) AS Narration, SUBSTRING(DateFrom, 7, 2) + SUBSTRING(DateFrom, 4, 2) + SUBSTRING(DateFrom, 1, 2) AS DateFrom, SUBSTRING(DateTo, 7, 2) 
                  + SUBSTRING(DateTo, 4, 2) + SUBSTRING(DateTo, 1, 2) AS DateTo, CAST(PrintNarration AS bit) AS PrintNarration, CAST(DiscAmt AS float) AS DiscAmt, 
                  CAST(ISNULL(CCAuthNo, N'') AS nvarchar(20)) AS CCAuthNo, CAST(ISNULL(CCTransID, N'') AS nvarchar(20)) AS CCTransID, CAST(UserLogin AS nvarchar(20)) 
                  AS UserLogin, CAST(Reconciled AS bit) AS Reconciled, SUBSTRING(DateReconciled, 7, 2) + SUBSTRING(DateReconciled, 4, 2) + SUBSTRING(DateReconciled, 1, 2) 
                  AS DateReconciled, CAST(PrimaryKey AS bigint) AS PrimaryKey, SUBSTRING(InvDate, 7, 2) + SUBSTRING(InvDate, 4, 2) + SUBSTRING(InvDate, 1, 2) AS InvDate, 
                  CAST(InvNo AS int) AS InvNo FROM         SomeDatabase.dbo.tblTransaction WHERE IsDate(trxTime) = 1
3
  • What is the programming language that you are using? Commented Aug 7, 2019 at 3:09
  • Testing it on Regexr, not necessarily for any language Commented Aug 7, 2019 at 3:16
  • With .NET regex you could use a vaiable width lookbehind: (?<= AS.*?), Commented Aug 7, 2019 at 11:54

3 Answers 3

1

You could try \K, but make sure to change Javescript in RegExr from top right of the screen to PCRE.

enter image description here

\K is defined as:

Sets the given position in the regex as the new "start" of the match. This means that nothing preceding the K will be captured in the overall match.

With \K, you could try something like this:

(?<= AS).*?\K(,)

Example: https://regex101.com/r/X3AdbH/1/

Sign up to request clarification or add additional context in comments.

1 Comment

That works, thanks! Still strange that we can't use generic lookaheads or non-capturing groups for that
1

If \K is supported, you could get your matches without using a lookbehind and a capturing group by matching AS and use a negated character class to match any char except a comma.

 AS [^,]+\K,

Explanation

  • AS Match space, AS and space
  • [^,]+ Match 1+ times any char except a comma
  • \K, Forget what was matched and match a comma

Regex demo

Comments

0

I'm guessing that your expression is just fine, you maybe want to limit the first capturing group to some specific chars, if you wish, maybe looking like:

(?<= AS)([A-Za-z\d\s]+)(,)

The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.