You may want to try "(?sm)import\s([^\n]+$|\{.*?\}$)". And then, you either filter the (single) capturing group base on whether it starts with braces, or you go for adding capturing subgroups in the expression above, and find the non-empty one.