First, because there is no code implementing your regex, there is no way for us to be certain that this question pertains to Ubuntu (is on-topic).
Second, I'll explain the error message because it can be a little misleading to developers who are encountering it for the first time. It is telling you that you failed to finish your pattern with another caret (^
). The truth is, if you merely add a caret to the end of your pattern it will damage the intended logic of your pattern. You see, the code that you are writing is expecting your pattern to be wrapped in delimiting characters - forward slashes are the most commonly used delimiters. To correct the syntax, just add a slash at the start and a slash at the end of your pattern.
Third, your pattern logic needs to be corrected. You don't want to use the beginning of string (^
) and end of string ($
) metacharacters for your needs. The solution is quite simple and straightforward with the use of "word boundary" meta characters (\b
). An online demonstration
/\b\w{3}\b/
No capture groups or lookarounds are necessary. \w
is a "word" metacharacter that matches [a-zA-Z0-9_]
which means any character in the following list: lowercase letters, uppercase letters, numbers, and underscore. The {3}
is a "quantifier" which demands that exactly 3 characters in the \w
range are matched. The \b
on either side of the \w{3}
ensures that the there is no character that could be matched by \w
immediately before or after the 3 matched characters.
Your sample input string doesn't look very realistic, so it is hard to confidently offer a pattern which executes tighter validation.
If you are new to regular expressions, this is a nice resource to read over: https://www.rexegg.com/regex-php.html