(?-imsx:^((?:[^_]+_){2}[^_]+)_) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- (?: group, but do not capture (2 times): ---------------------------------------------------------------------- [^_]+ any character except: '_' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- _ '_' ---------------------------------------------------------------------- ){2} end of grouping ---------------------------------------------------------------------- [^_]+ any character except: '_' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- _ '_' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------