Regex: match pattern as long as it's not in the beginning

You can use a look behind to make sure it is not at the beginning. (?<!^)aaa


Since I came here via Google search, and was interested in a solution that is not using a lookbehind, here are my 2 cents.

The [^^]aaa pattern matches a character other than ^ and then 3 as anywhere inside a string. The [^...] is a negated character class where ^ is not considered a special character. Note the first ^ that is right after [ is special as it denotes a negation, and the second one is just a literal caret symbol.

Thus, a ^ cannot be inside [...] to denote the start of string.

A solution is to use any negative lookaround, these two will work equally well:

(?<!^)aaa

and a lookahead:

(?!^)aaa

Why lookahead works, too? Lookarounds are zero-width assertions, and anchors are zero-width, too - they consume no text. Literally speaking, (?<!^) checks if there is no start of string position immediately to the left of the current location, and (?!^) checks if there is no start of string position immediately to the right of the current location. The same locations are being checked, that is why both work well.