The regular expression: (?-imsx:.(?=(R.L))) 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): ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- R 'R' ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- L 'L' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------