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

StackOverflowNewbie picture StackOverflowNewbie · Mar 27, 2013 · Viewed 30.8k times · Source

Assume the following strings:

aaa bbb ccc
bbb aaa ccc

I want to match aaa as long as it is not at the start of the string. I'm trying to negate it by doing something like this:

[^^]aaa

But I don't think this is right. Using preg_replace.

Answer

Joe picture Joe · Mar 27, 2013

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