blackened
blackened

Reputation: 903

Regex: Non-capture group

[Regex beginner] In Sublime Text 3, how do I find, say, aa such that there is no a on the immediate left and right, and replace it with aaa? I tried:

(?:[^a])(aa)(?:[^a])

but that selects four characters. How do I select the aa only?

Upvotes: 3

Views: 227

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627371

You can use a combination of a negative lookbehind and lookahead:

(?<!a)aa(?!a)

The (?<!a) fails the match if there is a a immediately to the left of the current location and (?!a) fails the match if there is an a immediately to the right of the current location.

See the PCRE regex demo (SublimeText3 uses PCRE engine).

Upvotes: 4

Related Questions