Community
    • Login

    Using OR in replace. How to adress given group?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 4 Posters 504 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.
    • Adam ProchowskiA
      Adam Prochowski
      last edited by

      Hello,

      When using find, you can do something like (X|Y) and notepad will find both of these. How can i adress these alternative groups in replace?

      A simplified example: Lets say we want to swap true and false in these lines:

      A candy is true .
      A candy is false .
      A candy is true .
      A candy is false .
      A candy is true .

      Find: (true)|(false)

      I tried using something like \2\1 or \1\2 in replace, but then it does nothing?
      How can i set replace option for each alternative?
      Again, this is only simplified example so 3-step replace is not satisfying :)

      1 Reply Last reply Reply Quote 0
      • rinku singhR
        rinku singh
        last edited by rinku singh

        @Adam-Prochowski said:

        When using find, you can do something like (X|Y) and notepad will find both of these

        find what : A candy is .*$

        Search mode: Regular expression

        1 Reply Last reply Reply Quote 0
        • Adam ProchowskiA
          Adam Prochowski
          last edited by Adam Prochowski

          @gurikbal-singh said:

          find what : A candy is .*$

          Search mode: Regular expression

          This changes whole subject to a small regex workaround that doesnt work as indented. I Want to have both alternatives and i want to replace based on which alternative is found. So if you find “true” replace with false, if you find “false” replace with true. this is again simplified. Once i get this to work, the alternatives will be contucted of regexes and the replace will be also based on groups i find using regex. I only pinpointed the part i have trouble with - How to adress alternative in find - so that people don’t go into understanding unnecessary commands when question is simple.

          Hope this clarifies the problem.

          1 Reply Last reply Reply Quote 0
          • AdrianHHHA
            AdrianHHH
            last edited by

            Try a regular expression replace of
            (A candy is true)|(A candy is false)
            with
            (?1A candy is false)(?2A candy is true)

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

            Adam ProchowskiA 1 Reply Last reply Reply Quote 2
            • Adam ProchowskiA
              Adam Prochowski @AdrianHHH
              last edited by Adam Prochowski

              @AdrianHHH said:

              Try a regular expression replace of
              (A candy is true)|(A candy is false)
              with
              (?1A candy is false)(?2A candy is true)

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

              Thanks! This works :) . I went to this page before but i must’ve misunderstood

              "For example, the format string “(?1foo:bar)” will replace each match found with “foo” if the sub-expression $1 was matched, and with “bar” otherwise. "

              I tried to use it with “:” between alternatives, but that didn’t quite work. Your solution with (?N smg ) works just right.

              1 Reply Last reply Reply Quote 1
              • Alan KilbornA
                Alan Kilborn
                last edited by

                A couple of more good references to this technique:

                • https://notepad-plus-plus.org/community/topic/12887/switching-letters-with-one-regex
                • https://notepad-plus-plus.org/community/topic/14157/change-a-word-in-regex
                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors