• Login
Community
  • Login

Using OR in replace. How to adress given group?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 4 Posters 519 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.
  • A
    Adam Prochowski
    last edited by Aug 30, 2019, 6:05 AM

    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
    • R
      rinku singh
      last edited by rinku singh Aug 30, 2019, 7:01 AM Aug 30, 2019, 7:00 AM

      @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
      • A
        Adam Prochowski
        last edited by Adam Prochowski Aug 30, 2019, 7:26 AM Aug 30, 2019, 7:24 AM

        @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
        • A
          AdrianHHH
          last edited by Aug 30, 2019, 8:11 AM

          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

          A 1 Reply Last reply Aug 30, 2019, 8:25 AM Reply Quote 2
          • A
            Adam Prochowski @AdrianHHH
            last edited by Adam Prochowski Aug 30, 2019, 8:25 AM Aug 30, 2019, 8:25 AM

            @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
            • A
              Alan Kilborn
              last edited by Aug 30, 2019, 12:02 PM

              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
              5 out of 6
              • First post
                5/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors