Community
    • Login

    need of explanation of find and replace with option regex

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    22 Posts 5 Posters 9.7k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • guy038G
      guy038
      last edited by guy038

      Hello, @andrea-seyfarth, @peterjones and All

      Here are two regexes :

      Regex A : (?s)^\h*II\x20.+?\R\h*VI\x20(?-s).+\R?

      Regex B : (?s)^\h*II\x20((?!Header).)+?Steuerklasse\x20VI(\R|\z)

      that I tested against the text below :

      111,11I Header
          I       000.00
          II      222,22
          III     333.33
          IV      444.44
          V       555.55  Steuerklassen V und IV
          VI      666.66  Steuerklasse VI
          VII     777.77  Steuerklasse VII
          VIII    888.88  Steuerklasse VIII
      111,11I Header
          I       000.00
          II      222,22
          III     333.33
          IV      444.44
          V       555.55  Steuerklassen V und IV
          VI      666.66  ----- NO MATCH 1 ----- VI
          VII     777.77  Steuerklasse VII
          VIII    888.88  Steuerklasse VIII
      111,11I Header
          I       000.00
          II      222,22
          III     333.33
          IV      444.44
          V       555.55  Steuerklassen V und IV
          VI      666.66  Steuerklasse VI
          VII     777.77  Steuerklasse VII
          VIII    888.88  Steuerklasse VIII
      111,11I Header
          I       000.00
          II      222,22
          III     333.33
          IV      444.44
          V       555.55  Steuerklassen V und IV
          VI      666.66  ----- NO MATCH 2 ----- VI
          VII     777.77  Steuerklasse VII
          VIII    888.88  Steuerklasse VIII
      111,11I Header
          I       000.00
          II      222,22
          III     333.33
          IV      444.44
          V       555.55  Steuerklassen V und IV
          VI      666.66  ----- NO MATCH 3 ----- VI
          VII     777.77  Steuerklasse VII
          VIII    888.88  Steuerklasse VIII
      111,11I Header
          I       000.00
          II      222,22
          III     333.33
          IV      444.44
          V       555.55  Steuerklassen V und IV
          VI      666.66  Steuerklasse VI
      

      The regex A catches the entire FIVE lines, beginning with Roman number II and beginning with Roman number VI ( with possible horizontal blank characters before ), whatever their contents

      And the regex B catches the entire FIVE lines, beginning with Roman number II ( with possible horizontal blank characters before ) and ending with the string Steuerklasse VI, ONLY IF the string Header cannot be found, at any position, of the smallest multi-lines sequence of characters, after the regex \h*II\x20 till the regex Steuerklasse\x20VI !

      So, Peter, as you can see, I used the negative look-ahead (?!Header), which is tested at any position of the . => the syntax ((?!Header).)+?. Note also, by I preferred to get the entire lines, with their End of Line chars ! So, when the replacement zone is empty, it does not remain any blank line, afterwards :-))

      I also, used the alternative (\R|\z), just in case the very last line would be a line VI 666.66 Steuerklasse VI, without any line-break !

      Best Regards,

      guy038

      1 Reply Last reply Reply Quote 1
      • PeterJonesP
        PeterJones
        last edited by

        The word “Header” was my invention, and not in any of @Andrea-Seyfarth’s examples, so that shouldn’t be used for a regex we suggest to her. Sorry for muddying the waters with my example file.

        I like your cleaner \h for the horizontal space (that escape sequence hadn’t yet stored in my long-term regex memory; some day, maybe even today, it will).

        Thanks for continually sharing your regex expertise with us. I’m always amazed by your expressions, and the quality of your explanations.

        Hopefully, we’ve helped Andrea in the process. :-)

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors