Regular Expression to exclude substring
I'm trying to parse through a string with regular expressions (.NET) and find all custom tokens starting with [[
and ending with ]]
. My first attempt was to use \[\[(.*)\]\]
. This seemed to work when there was only one token in a string. But if there were multiple, it just return one result from the first [[
to the very last ]]
.
My thought is to exclude ]]
from matching characters, but I've yet to find a way to get that working. I've tried usi开发者_运维问答ng exclusion sets (?! \]\])
and played around with different syntax, but can't find anything that actually works.
Anyone know an easy way to do this?
Regular expressions are greedy by default, i.e. they consume as many characters as possible. To avoid this put a "?" after the ".*", i.e. try \[\[(.*?)\]\]
.
It's even simpler than that, try using .*?
(the last ? means it is an ungreedy match, meaning it will grab the bare minimum when catching the information).
For reference, please check out this site on regex. It will give you more details on greedy vs. ungreedy.
.* is a "greedy" match, and goes to the last match of your brackets.
*? specifies the first match that consumes as few repeats as possible (equivalent to lazy *)
精彩评论