Community
    • Login

    is regex condition possible in replace-string

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 7.2k 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.
    • Jos MaasJ
      Jos Maas
      last edited by

      I convert genealogic indexes from archive-sites into a ged-file, so they can be imported in my genealogic program.

      In the searchstring I have made a named expression called (?<CTNOTE>…). In some cases it is possible that the CTNOTE expression is an empty string.

      So I want to insert the lines 2 NOTE @N01@ and 0 @N10@ NOTE $+{CTNOTE} in the replaced result text only when the CTNOTE expression is not empty. Is it possible to write an replace-regex that does this job and how is it written down?

      1 Reply Last reply Reply Quote 0
      • MAPJe71M
        MAPJe71
        last edited by

        Again, this is more a RegEx related question.
        There are several websites dedicated to RegEx, just google for it.

        1 Reply Last reply Reply Quote 1
        • guy038G
          guy038
          last edited by

          Hi, @jos-maas and All

          Sorry, I didn’t understand, exactly, what string must replace your matched search, but here is the general description of the back-references and conditional replacements syntaxes, in the REPLACE part, when using named groups :

          SEARCH (?-s)(?<jos>^\d+$)|.+

          REPLACE (?{jos}Rewrite $+{jos}, as Group 'jos' exists:Group 'jos' does NOT exist !)

          With the reges S/R, above, the text :

          123
          This is a test
          4567890
          

          would be changed into :

          Rewrite 123, as Group 'jos' exists
          Group 'jos' does NOT exist !
          Rewrite 4567890, as Group 'jos' exists
          

          Notes :

          • As usual, the (?-s) part is added to be sure that the dot symbol will match standard characters, only !

          • Then we’re searching, either :

            • A single integer number, per line, stored as group jos (?<jos>^\d+$) ( It’s the priority search, as the first branch of the alternative )

            • All NON-empty contents of current line .+

          • In replacement, we use the conditional replacement (?{jos}++++++++:--------), which means :

            • If the group jos exists, we rewrite the ++++++++ text ( part before the colon symbol )

            • If the group jos does not exist, we rewrite the -------- text ( part after the colon symbol )

          So :

          • If a line contains a single number, the group jos exists and we re-write Rewrite $+{jos}, as Group 'jos' exists, with $+{jos, being a back-reference to the named group jos

          • ELSE the group jos does not exist and we, simply, rewrite the literal text, located after the colon symbol Group 'jos' does NOT exist !

          I do hope that this simple example will help you to solve your problem, easily !

          Cheers,

          guy038

          1 Reply Last reply Reply Quote 0
          • Jos MaasJ
            Jos Maas
            last edited by

            Bonjour, Guy038,

            Your help was to the point, as showed by snippets from the search and replace string hereafter.

            In the searchstring: (Opmerking\R+(?<CTOPM>[A-z ]*$)\R+) - there is no need to have an alternation operator and string, because there is a match if “Opmerking\R” is in the tekst and the CTOPM-named substring is not empty, or not.

            The replacestrings: (?{CTOPM}2 NOTE @N10@\r\n:) and (?{CTOPM}0 @N10@ NOTE $+{CTOPM}\r\n:) generate the wanted lines if there is a match, or do nothing if there is no match.

            This item and that in my previous post illustrate the problem that I have as a newby in regex matters. As I know from my studies - some fifty years ago - you need basic theory, advanced theory and applied theory books to learn a job. I feel there is a lack specific for the regex in notepad++. The tutorial could be seen as an applied handbook, your “SYNTAXE des expressions RÉGULIÈRES PRCE, de NOTEPAD++ v6.0 et PLUS” is perhaps advanced theory, but - as far as I can see - both don’t give a complete overview of all aspects of the regex in notepad++. So far your replies on my posts provide the needed information in addition.

            I am grateful for your help on both my posts and I surely will write a call for help when I encounter a new problem.

            Cordialement, Jos

            1 Reply Last reply Reply Quote 0
            • guy038G
              guy038
              last edited by guy038

              Hi, @jos-maas,

              Oh yes ! You’re perfectly right. Even, in the example, described in my previous post, we don’t need any alternative !

              But, in that case, when a line does not match ( NO group jos ), this line stays unchanged, and we do not need, either, the ELSE part of the conditional replacement !

              So, the previous regex S/R, becomes :

              SEARCH (?-s)(?<jos>^\d+$)

              REPLACE ?{jos}Rewrite $+{jos}, as Group 'jos' exists

              And, with the original text :

              123
              This is a test
              4567890
              

              We would get, this time, the text :

              Rewrite 123, as Group 'jos' exists
              This is a test
              Rewrite 4567890, as Group 'jos' exists
              

              Note that, in replacement, the outer parentheses are removed, without any problem !


              Jos, for further information, about regular expressions concept and syntax, begin with that article, in N++ Wiki :

              http://docs.notepad-plus-plus.org/index.php/Regular_Expressions

              In addition, you’ll find good documentation, about the Boost C++ Regex library, v1.55.0 ( similar to the PERL Regular Common Expressions, v1.48.0 ), used by Notepad++, since its 6.0 version, at the TWO addresses below :

              http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

              http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

              • The FIRST link explains the syntax, of regular expressions, in the SEARCH part

              • The SECOND link explains the syntax, of regular expressions, in the REPLACEMENT part


              You may, also, look for valuable informations, on the sites, below :

              http://www.regular-expressions.info ( The reference !! )

              http://www.rexegg.com

              http://perldoc.perl.org/perlre.html

              Be aware that, as any documentation, it may contain some errors ! Anyway, if you detected one, that’s good news : you’re improving ;-)

              Cheers,

              guy038

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