user1612851
user1612851

Reputation: 1214

Regex - Non capturing group not working

I am using regex in Powershell. I have a non-capturing group but it is still showing in my results.

String is:

Grid1.Data = [['TRE - Significant Flux','EG Report','w939909','59e8a31f-d9e9-4ebf-a027-929ec62953ac'],['CB - Daily OD Report','EG Report','w9
39909','59e8a31f-d9e9-4ebf-a027-929ec62953ac'],['BC - Balance Transfers Daily - DRAFT','BC - Balance Transfers Daily - DRAFT','w939909','59e8
a31f-d9e9-4ebf-a027-929ec62953ac'],['CB - CL Activity Report','CB - CL Activity Report','w939909','59e8a31f-d9e9-4ebf-a027-929ec62953ac']]

Regex is:

$regex = "(?:\[').*?(?=')"  

It is still returning things like ['TRE - Significant Flux

I need to exclude the opening "['". The non capturing group at the end for the single quote is working.

Upvotes: 4

Views: 6375

Answers (2)

Uber Kluger
Uber Kluger

Reputation: 502

The non-capturing group (?:\[') is still included in the "matching" part of the expression and so is returned in the "match". Lookaheads (e.g. (?=')) and lookbehinds are not matching parts but boolean assertions (like anchors ^ and $). Their pattern must match (positive look) or not match (negative look) from the current string position forwards (lookahead) or backwards (lookbehind) but they return only true or false and do not advance the match pointer. The term "non-capturing" refers to the use of parentheses to "capture" separately retrievable subsections (chunks) of an overall match and also to group parts of a pattern, typically when quantifiers (*, +, ?) are applied to otherwise ungrouped sections, e.g. parts separated by |. A "non-capturing" group ( (?:regex) ) performs the grouping function but does not capture anything (and does not add to the list of captures).

Note that the 2nd example given in @CB's answer is not minimal. Since the requirement is to retrieve the text between the two ' characters immediately after a [ and the final ' is to be discarded (not required for subsequent matches), the use of a capturing group means that including the [ and two ' characters in the primary match is irrelevant. Thus the pattern can be

$regex = "\['(.*?)'"

Upvotes: 1

CB.
CB.

Reputation: 60976

try using lookbehind assertion

$regex = "(?<=\[').*?(?=')"

or:

$regex = "(?:\[\[')(.*?)(?=')"

$yourstring -match $regex

$Matches[1]

Upvotes: 5

Related Questions