You need backreferences. The idea is to use a capturing group for the first bit, and then refer back to it when you're trying to match the last bit. Here's an example of matching a pair of HTML start and end tags (from the link given earlier):
<([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>
This regex contains only one pair of parentheses, which capture the string matched by [A-Z][A-Z0-9]*
into the first backreference. This backreference is reused with \1
(backslash one). The /
before it is simply the forward slash in the closing HTML tag that we are trying to match.
Applying this to your case:
/^(.{3}).*\1$/
(Yes, that's the regex that Brian Carper posted. There just aren't that many ways to do this.)
A detailed explanation for posterity's sake (please don't be insulted if it's beneath you):
^
matches the start of the line.
(.{3})
grabs three characters of any type and saves them in a group for later reference.
.*
matches anything for as long as possible. (You don't care what's in the middle of the line.)
\1
matches the group that was captured in step 2.
$
matches the end of the line.