Regsub Match Line V4



Title: Regsub Match Line V4


gold Note to clean up crew. Checking end of line issues here from laptop PC editor. If one sees a syntax error or brace typo error, go ahead and change. My eyes are bad on these tiny fonts.


Find a text in a line and replace the next line


gold 3/26/2024. Found example code for regsub on Ask, and it shall be given # 11. Seemed a useful lesson and example code on regsub and replace, if category links and Wiki page references were added to find same in the Wiki stacks.


Question: Working on a Text File V2


tclamateur : I want to find a text in a line and replace the contents of the next line with a new string. I tried regsub, but it is only matching the first line. I don't know how to go to the next line. eg:


line 5> this is line 5.

line 6> apple


with

line 5> this is line 5.

line 6> banana

Thanks


First Example


AMG: [regsub] has the -all option to replace every occurrence, not just the first.


proc replace {match replacement text} {
    # Backslash-quote special characters in match and replacement so they will
    # be interpreted as literal strings.
    regsub -all {[][*+?{}()<>|.^$]} $match {\\&} match
    regsub -all {[\\&]} $replacement {\\&} replacement

    # Perform the replacement.
    regsub -all ($match\\n)\[^\\n\]* $text \\1$replacement
}

replace "this is line 5." banana {
this is line 5.
apple
this is line 5.
another apple
this is not line 5.
yet more apples
}

This gives the following result:


none
this is line 5.
banana
this is line 5.
banana
this is not line 5.
yet more apples

Second Answer


MG offers:


proc replace2 {args} {

if { [llength $args] < 3} {
     return -code error "wrong # arguments: replace2 ?-all? ?-regexp|-glob|-exact? \$find \$replace \$text"
   }
set find [lindex $args end-2]
set replace [lindex $args end-1]
set text [lindex $args end]
set args [lrange $args 0 end-3
]
set matchtype -exact
set all ""
foreach x $args {
if {$x eq "" } {
    continue;
   } elseif { $x eq "-all" } {
     set all [list "-all"]
   } elseif { $x in [list -regexp -glob -exact] } {
     set matchtype $x
   } else {
     return -code error "unknown option '$x'"
   }
}
set text [split $text \n]
set matches [lsearch {*}$all $matchtype $text $find]
if { [llength $matches] && [lindex $matches 0] != -1 } {
     foreach x $matches {
        incr x
        set text [lreplace $text $x $x $replace]
     }
   }
return [join $text "\n"]

}

# Example:
replace2 -all -exact "this is line 5." banana {
this is line 5.
apple
this is line 5.
another apple
this is not line 5.
yet more apples
}

Two Questions for One, File 13


tclamateur: Thanks for the input. In my case I'll have only two inputs. The input "match" and the input "replacement". What to give as input for "text"?


AMG: Whatever text you want the substitution to be performed on. For instance, if you're working on a file, you'd supply the contents of that file. Then write the return value back out to the file, and you're done.


Boilerplate Outline on Regsub Command


The regsub command in Tcl (Tool Control Language) performs substitutions based on regular expression pattern matching. Let’s dive into its usage and provide some examples:


Basic Syntax:


regsub ?switches? exp string subSpec ?varName?


exp: The regular expression pattern.


string: The input string to match against.


subSpec: The substitution specification.


varName (optional): If provided, the resulting string is stored in this variable, and the number of substitutions is returned. Switches:


-all: Causes regsub to perform the replacement in as many non-overlapping places as possible.


Other switches (e.g., -nocase, -start index) are similar to those for regexp.


Examples: a. Replace a specific substring in a file name:


set filename "file.c"
regsub -- {([^\\.]+)\\.c} $filename {cc -c & -o \\1.o}
# Result: "cc -c file.c -o file.o"

b. Convert a string template:

set input "rand ||=> this is some text <=|| rand"
set unique1 {\\|\\|=>}
set unique2 {<=\\|\\|}
set replacement {some other text}
set new [regsub -- "($unique1).*($unique2)" $input "\\1$replacement\\2"]
puts $new
# Result: "rand ||=> some other text <=|| rand"

c. Custom substitution using -command:

set string "ab-cd-ef-gh"
regsub -all -command {w} $string {puts}
# Prints the letters 'a' to 'h', one per line.

d. More advanced example using -command:


set input "1234A000aadA12234"
set substring "BX"
regsub -all -command {(A[^A]*A)} $input {[string repeat $substring [expr {[string length "\\1"] / [string length $substring]}]]} new_string
puts $new_string
# Result: "1234BXBXBXBX12234"

Remember that regsub is a powerful tool for manipulating strings using regular expressions. Feel free to experiment and adapt it to your specific needs! For more details, one can refer to the official Tcl documentation core.


References on Wiki


regsub, a built-in Tcl command, performs substitutions based on regular expression pattern matching.

See Also

regular expressions
information about Tcl regular expressions that is not unique to any particular command
string map
subst

Synopsis

regsub ?switches? exp string subSpec ?varName?

Extra Credit References



Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 3/26/2024



gold 3/27/2024. Note to clean up crew. Checking end of line issues here from laptop PC editor. If one sees a syntax error or brace typo error, go ahead and change. My eyes are bad on these tiny fonts.