Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 2 - Regex Search - Non-Capture Group Syntax

I'm trying to use ST2's regex capability in search & replace, but can't figure out how to probably make a non-capturing group. For this example, I want to find instances of "DEAN" which are not followed by "UMBER", i.e. to distinguish "DEANCARE" from "DEANUMBER"

From what I've read and used in the past, the syntax with a non-capture should be:

DEAN(?:UMBER)

Which should match "DEANCARE" but not "DEANUMBER". Yet instead, Sublime Text only finds "DEANUMBER" as if I had typed:

DEAN(UMBER)

Using square brackets on the first (or each) of the unwanted letters does work:

DEAN[^U] 

But I'd still prefer to use the group non-match as opposed for other purposes and to avoid having to explicitly not-match each individual character. Do I have a syntax mistake, or maybe a conceptual error in how ST2's regex works?

like image 522
TCAllen07 Avatar asked Dec 01 '25 00:12

TCAllen07


1 Answers

A non capturing group is the same as a group except it does not capture the matching portion of the regex in a back-reference.

If you were to use the regex DEAN(?:UMBER) on the string DEANUMBER then you would have a match, but referencing \1 in, e.g. a search and replace would give you nothing, because the group is non-capturing.

Using DEAN(UMBER) on the other hand you could do a search and replace with made of L\1 which would produce made of LUMBER because the match of the first (capturing) group is being back-referenced by \1. This of course is a very pointless example, if you want to learn more about groups and back-referencing I'd suggest you read this or some other documentation/turoial on the matter.

As suggested in the comments, what you want is a negative lookahead.

like image 200
rvalvik Avatar answered Dec 02 '25 13:12

rvalvik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!