Solve a nasty regular expression problem? Share your examples for the benefit of others here. Please include the problem statement, the code and then any discussion. See [Regular Expression Examples] for the easy examples. ---- '''Problem Statement: ''' Modify a string with a series of substrings enclosed in double square brackets ([[ ]]) such that they become only single brackets. Example input: [[X]] [[abc]] [[123]] Desired output: [X] [abc] [123] The actual substrings could be any arbitrarily nasty text, including such things as &, \1 etc. '''Code: ''' regsub -all {\[\[(.*?)\]\]} $thebody {[\1]} thebody '''Discussion: ''' The non-greedy expression .*? will expand all text until the first match of ]]]]. Works great. This example comes from [Wiki Conversion OddMuse To Confluence] where you can find the entire example. ''[Lars H]: Doesn't look particularly advanced to me — the only nontrivial aspect is the use of a non-greedy quantifier. Occurencies of regexp syntax characters in the string operated on has never been a problem (but including them in the regexp pattern can be). Also, one might wonder whether the following [string map] isn't an easier way to do this:'' string map {[[ [ ]] ]} $thebody ''Either way, the edge cases occur when $thebody contains brackets that aren't string delimiters. Possibly strings containing newlines can also be troublesome.'' ---- '''Problem Statement: ''' Modify a string containing a series of substrings enclosed in double square brackets ([[[[ ]]]]) such that any spaces in that substring are converted to underscores (_). '''Code: ''' ====== while { [regsub -all {(\[\[[^] ]+) (.+?\]\])} $thebody {\1_\2} thebody] > 0 } {} OR set idx [regexp -inline -all -indices {\[[^]]*\]} $txt] foreach pair $idx { foreach {start end} $pair { break } set new [string map [list { } _] [string range $txt $start $end]] set txt [string replace $txt $start $end $new] } ====== '''Discussion: ''' In the first line, a complete iterative approach is taken. Where there are many more substrings than there are spaces in those substrings, this method clearly computationally efficient. This example comes from [Wiki Conversion OddMuse To Confluence] where you can find the entire example. If there are more spaces than substrings then, at some point, it becomes more computationally effficient to follow the second set of statements. Those statements were constructed by another person on the Tcler's Chat. There does not seem to be a way to solve this problem in a single step. ---- *** Recommended Template '''Problem Statement: ''' '''Code: ''' ====== ====== '''Discussion: ''' ----