Community
    • Login

    [regex help] I want to find whatever is inside brackets "[ ]" and the brackets themselves

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 3.3k 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.
    • codenotworkingC
      codenotworking
      last edited by

      This is the basic regex i have “(?=.)[[\w-\s]”.

      I want to be able to find what’s inside the brackets “[ ]” and the brackets themselves, so I can replace them with whatever I wish. For example:

      lorem ipsum [lorem-ipsum lor-em ip sum] 12345end --> lorem ipsum REPLACED TEXT 12345end

      Any help would be appreciated.

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @codenotworking
        last edited by

        @codenotworking

        The basic regex you showed is F’d up due to markdown syntax used on this site.

        I would try this regex:

        \[[^]]+\]

        It looks very strange but it just might work. Note that it isn’t overly restrictive, pretty much anything inside the square brackets and the brackets themselves will be selected.

        codenotworkingC 1 Reply Last reply Reply Quote 4
        • guy038G
          guy038
          last edited by guy038

          Hello, @codenotworking, @alan-kilborn, and All,

          If you don’t want the regex to match multi lines [.............] blocks, which begins on a line and ends, on a further line, change the Alan Kilborn’s regex to this new one :

          \[[^]\r\n]+\]


          A second formulation could be :

          • (?s)\[.+?\], which allows multi-lines [.............] blocks

          • (?-s)\[.+?\], which allows mono-lines [.............] blocks, only

          The first regex searches for the smallest range of any character, including EOL ones, different from, either, [ and ], after the [ symbol, till the ] symbol

          The second regex searches for the smallest range of any standard character,( not line-breaks ), different from, either, [ and ], after the [ symbol, till the ] symbol


          BTW, @codenotworking, + 1 for your name’s choice ! The true nightmare of any coder ;-))

          Best Regards,

          guy038

          Alan KilbornA 1 Reply Last reply Reply Quote 5
          • Alan KilbornA
            Alan Kilborn @guy038
            last edited by

            @guy038

            Yep…but so many things left unspecified by the OP…

            1 Reply Last reply Reply Quote 2
            • codenotworkingC
              codenotworking @Alan Kilborn
              last edited by

              @Alan-Kilborn
              Thanks. This works perfectly for my needs.

              @guy038
              Thanks for the suggestion and the props.

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