Regex: Match opening/closing chars with spaces

Posted by Israfel on Stack Overflow See other posts from Stack Overflow or by Israfel
Published on 2010-03-18T14:33:47Z Indexed on 2010/03/18 14:41 UTC
Read the original article Hit count: 141

I'm trying to complete a regular expression that will pull out matches based on their opening and closing characters, the closest I've gotten is

^(\[\[)[a-zA-Z.-_]+(\]\])

Which will match a string such as "[[word1]]" and bring me back all the matches if there is more than one, The problem is I want it to pick up matchs where there may be a space in so for example "[[word1 word2]]", now this will work if I add a space into my pattern above however this pops up a problem that it will only get one match for my entire string so for example if I have a string

"Hi [[Title]] [[Name]] [[surname]], How are you"

then the match will be "[[Title]] [[Name]] [[surname]]" rather than 3 matches "[[Title]]", "[[Name]]", "[[surname]]". I'm sure I'm just a char or two away in the Regex but I'm stuck, How can I make it return the 3 matches.

Thanks

© Stack Overflow or respective owner

Related posts about regular-expressions

Related posts about ASP.NET