regexregex-lookaroundsregex-negation

regex: negative lookahead with multiline text


I have several files with text blocks like that:

  maxlength:
    maxlength_js: 500
    maxlength_js_label: 'Inhalt auf @limit Zeichen begrenzt, verbleibend: <strong>@remaining</strong>'
    maxlength_js_enforce: true
    maxlength_js_truncate_html: false

Sometimes maxlength_js_enforce: true is present sometimes not. I want to add maxlength_js_enforce: true to all such maxlenght: blocks by find and replace.

I tried to find all maxlength: blocks without maxlength_js_enforce with a regex pattern with negative lookahead but somehow I can't get it working with multiple lines and no real limit for the end of such a block.

I have tried it with a pattern like that (maxlength:(\s*.*))(?!enforce) Here is an example with a whole file with multiple occurences of such a block https://regexr.com/7mjh1 Make sure to open the whole file example to understand the problem.

Either it matches to many lines or not enough and even while "enforce" is present it is a match. I think I don't get the concept correct and have problems with specifing the borders of such a block in the file, guess it must be really simple but don't get it.


Solution

  • You can use

    (?m)^(\h*)maxlength:(?:\R\1(?!\h*maxlength_js_enforce:)(\h+).*)*$(?!\R\1\h)
    

    Then, you need to replace with

    $0\n$1$2maxlength_js_enforce: true
    

    See the regex demo.

    Details:

    The $0\n$1$2maxlength_js_enforce: true replacement pattern replaces the matched text with itself ($0) and then adds a newline (\n), Group 1 value ($1), Group 2 value ($2) and then the maxlength_js_enforce: true text.