[regex help] I want to find whatever is inside brackets "[ ]" and the brackets themselves
-
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.
-
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.
-
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]
symbolThe 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
-
-
Yep…but so many things left unspecified by the OP…
-
@Alan-Kilborn
Thanks. This works perfectly for my needs.@guy038
Thanks for the suggestion and the props.