Community
    • Login

    Regex replacement of text within regex find

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 548 Views 1 Watching
    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.
    • Nick MinchinN Offline
      Nick Minchin
      last edited by

      I want to find text using regex and then replace some of the characters within the found strings…
      e.g. I have this text:

         "dataLabels": {
              "show": true,
              "name": {
                  "show": true,
                  "fontSize": "16px",
                  "fontFamily": "undefined",
                  "fontWeight": 600,
                  "color": "undefined",
                  "offsetY": -10
              },
              "value": {
                  "show": true,
                  "fontSize": "14px",
                  "fontFamily": "undefined",
                  "fontWeight": 400,
                  "color": "undefined",
                  "offsetY": 16,
                  "formatter": "function (val) { return val + " % " }"
              },
              "total": {
                  "show": false,
                  "label": "Total",
      

      and this regex expression:

      "formatter": function([\s\S]+?)}
      

      This will find this:

      "formatter": "function (val) { return val + " % " }"
      

      I want to replace the " % " with ' % '
      There could be any number of these formatter finds I need to replace.

      Not sure if it’s possible to do with npp?

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA Offline
        Alan Kilborn @Nick Minchin
        last edited by

        @Nick-Minchin

        Reading between the lines, I would infer that you want to replace " % " but only when it occurs on the same line as "formatter": "function between exactly that and }" + line-ending. Is that correct?

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

          Hello @nick-minchin, @alan-kilborn and All,

          Here is the road map for this S/R :

          • Open your file in Notepad++

          • Go to the very beginning of this file ( Ctrl + Home )

          • Open the Replace dialog ( Ctrl + H )

          • SEARCH (?x-si) ( ^ \h* "formatter":\x20+"function | (?!\A) \G ) .*? \K " (?!$)

          • REPLACE '

          • Un-tick all options

          • Select the Regular expression search mode

          • Click on the Replace All button ( Do not use the Replace button !)

          Voila !

          This S/R will replace any non-trailing double quote ", only after the string "formatter": "function, with a simple quote ', in all the lines of your file

          Best Regards

          guy038

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors