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