Version 2 of Advanced Regular Expression Examples

Updated 2008-07-23 22:22:54 by buchs

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.


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: