• Login
Community
  • Login

rereplace help

Scheduled Pinned Locked Moved General Discussion
3 Posts 2 Posters 373 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.
  • T
    Trey
    last edited by Apr 23, 2025, 7:59 PM

    I’m trying to automate some of my coding tasks using pythonscript and running into an issue I think regex should be able to solve. I want to replace things that are specifically surrounded by certain characters while preserving the middle part.

    so for example:

    " & Placeholder &"

    becomes:

    {Placeholder}

    I thought that named capture variables would be able to do this but I am getting an empty {} trying to use them in this situation with the code:

    editor.rereplace(r" & \b(?<Placeholder_Variable>[a-zA-Z0-9._]*)\b & “,r”{(?{Placeholder_Variable})}")

    A 1 Reply Last reply Apr 24, 2025, 10:14 AM Reply Quote 1
    • A
      Alan Kilborn @Trey
      last edited by Alan Kilborn Apr 24, 2025, 10:36 AM Apr 24, 2025, 10:14 AM

      @Trey

      You have several things wrong in your regular expression.

      Try this instead:

      editor.rereplace(r'" & (?<Placeholder_Variable>\w+) &"', r'{$+{Placeholder_Variable}}')

      EDIT: Oops, the first version of this posting used Python named capture group stuff; I’ve adjusted to use Boost equivalents.

      T 1 Reply Last reply Apr 24, 2025, 3:07 PM Reply Quote 3
      • T
        Trey @Alan Kilborn
        last edited by Apr 24, 2025, 3:07 PM

        @Alan-Kilborn I had to re-add ‘.*’ to the middle section, but I got it working now and can use variables the way I want now, thanks!!

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