• Login
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.
  • C
    codenotworking
    last edited by Jul 3, 2019, 10:48 PM

    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.

    A 1 Reply Last reply Jul 3, 2019, 11:29 PM Reply Quote 0
    • A
      Alan Kilborn @codenotworking
      last edited by Jul 3, 2019, 11:29 PM

      @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.

      C 1 Reply Last reply Jul 5, 2019, 7:58 PM Reply Quote 4
      • G
        guy038
        last edited by guy038 Jul 4, 2019, 5:26 PM Jul 4, 2019, 5:13 PM

        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

        A 1 Reply Last reply Jul 4, 2019, 6:00 PM Reply Quote 5
        • A
          Alan Kilborn @guy038
          last edited by Jul 4, 2019, 6:00 PM

          @guy038

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

          1 Reply Last reply Reply Quote 2
          • C
            codenotworking @Alan Kilborn
            last edited by Jul 5, 2019, 7:58 PM

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

            @guy038
            Thanks for the suggestion and the props.

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