• Login
Community
  • Login

Regex replacement of text within regex find

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
3 Posts 3 Posters 280 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.
  • N
    Nick Minchin
    last edited by Sep 14, 2022, 8:54 PM

    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?

    A 1 Reply Last reply Sep 14, 2022, 9:07 PM Reply Quote 0
    • A
      Alan Kilborn @Nick Minchin
      last edited by Sep 14, 2022, 9:07 PM

      @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
      • G
        guy038
        last edited by guy038 Sep 15, 2022, 2:09 AM Sep 15, 2022, 2:04 AM

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