Reputation: 1291
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 using exclusion sets (?! \]\])
and played around with different syntax, but can't find anything that actually works.
Anyone know an easy way to do this?
Upvotes: 0
Views: 731
Reputation: 12861
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 \[\[(.*?)\]\]
.
Upvotes: 2
Reputation: 4651
.* 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 *)
Upvotes: 0
Reputation: 101604
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.
Upvotes: 0