Community
    • Login

    How to refer named group in RegExp-replacement?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 4 Posters 959 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.
    • AntonA
      Anton
      last edited by

      I want to use named group in RegExp to replace it with some text.
      I can’t understand how to refer this group in “replace”-textbox.
      I tried this way but failed:

      b8f80633-1c1a-42a8-a34d-56347a4f8716-изображение.png

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Anton
        last edited by

        @Anton ,

        As it says in the Searching > Regular Expressions > Substitutions section of the user manual

        dda252d0-bd9c-4cbe-b23d-a5e10c5816cb-image.png

        \<hi>

        That is never a valid syntax. I assume you were trying for the “backreference” notation of \g<hi> or \k<hi>. But as the backreference user manual section says,

        4c943e3e-4f86-4100-8710-41c671d1c365-image.png ,

        so even if you had used the right syntax for a backreference, it would not work in a replacement.

        mkupperM 1 Reply Last reply Reply Quote 3
        • mkupperM
          mkupper @PeterJones
          last edited by

          @PeterJones I was curious about this and so experimented: The manuals’s Capture Groups and Backreferences section documents three ways of defining the names: (?<name>subset) or (?'name'subset) or (?(name)subset) ⇒ Named Capture Group:

          The first two work for me:
          Search: (?<hi>world)
          Replace: $+{hi}
          and
          Search: (?'hi'world)
          Replace: $+{hi}

          This fails with Invalid Regular Expression starting at the second (
          Search: (?(hi)world)
          Replace: $+{hi}

          The Boost manual section in this only mentions the two forms that work.

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

            @mkupper said in How to refer named group in RegExp-replacement?:

            only mentions the two forms that work.

            You are right. And I confirmed that’s been the case in Boost for the older versions used by N++ throughout my tenure on the User Manual… so apparently the section on named capture groups has always had that extra third group that shouldn’t’ve been there.

            Interestingly, just because the Boost docs doesn’t list it doesn’t mean it’s not invisibly supported by that library: \g'name' or \k'name' as a backreference syntax aren’t mentioned, but it does work. But in this case, (?(name)...) is neither documented nor supported.

            This is being fixed in PR#573

            1 Reply Last reply Reply Quote 4
            • Mark OlsonM
              Mark Olson
              last edited by

              While we’re here, I’d like to talk about a related useful concept, namely how to reference a named pattern (but not the value captured earlier), which is extremely useful if you have some annoyingly long pattern that you need to match multiple times in the same regex.

              For example,
              (?'int'[-+]?\d+)\h*b+\h*(?&int)
              matches an integer with leading + or minus, some b characters, and then another integer (referenced with (?&int))

              I won’t expound too much on this topic because it’s also in the documentation, but I’ve definitely written many over-long regexes that could have been hugely simplified by this method.

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