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 5.6k 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.
    • codenotworkingC Offline
      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 Offline
        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 Offline
          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 Offline
            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 Offline
              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

              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