It is driving me crazy and I have no idea what I am doing wrong. In my document I have to autoformat some regular expressions in and including the brackets.

An example is here;

[DP, 07.04.1911, „Protestanten“]

DP could also be BSZ, the date varies and the string in the quotation marks too.

I tried following grep expression:


This is working in all cases where there is only one such sequence in the paragraph. If there is a second it starts at the opening bracket of the first sequence and stops after the closing bracket of the second and everything in between those sequences is selected too.

What am I doing wrong? I tried to use the closing quotation mark as a stop, but it gives the same result. Here is the grep expression:


  • 1
    Quick comment .* is greedy. So it will also match what comes afer them if theres a second instance of what comes after somewhere inthe text. Try .*? instead
    – joojaa
    Mar 8 '16 at 22:42

By default, GREP is greedy: as long as there is a match to be found further on, it will grab as much as possible. Since the . does not match (by default) a hard return character, it will usually scan all the way up to the end of a paragraph. If you enable 'single line mode' using (?s), then the . wildcard will even match hard returns and so tries to select all the way up to the end of your text.

There are two possible tricks to counteract this. First off is not using the plain "match-anything" . but a negation of the end character:


This will select any run of "not-] characters" and thus stop at the first occurrence of ], after which it will match the next character, which will always be the next ].

A more usual way, however, is to instruct GREP to use the shortest possible match, instead of the default longest possible. That is done by adding the extra modifier ? after one of the "repeat" codes ?, +, * or {x,y}. In your case, this would be


Note that to just format the text inside square brackets, the rightmost set of round parentheses is not necessary. You can simply use


because the quantity modifiers only act on the immediately preceeding group ((a|b)), character class ([abc]) or (in this case) single character or single character code.

  • Works like a charm! Thx a lot...
    – maegmaeg
    Mar 8 '16 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.