This question is about understanding the behaviour of a specific regex in TCL 8.5 built into Vivado, in particular or
-ing together two regex parts I get unexpected results:
I worked on indenting a block of text for the command line using regular expressions. My first thought was to replace every newline
by a newline
and some spaces
(replaced by X
here for clarity) for indentation, so:
puts [regsub -all "\n" "foo\nBar\nBaz" "\nXX"]
foo
XXBar
XXBaz
This does not indent the first line, to match the first line I use ^
:
puts [regsub -all "^" "foo\nBar\nBaz" "\nXX"]
XXfoo
Bar
Baz
Now it should just be a matter of comibining the two regex parts with an |
, however I get output I can not explain:
puts [regsub -all "^|\n" "foo\nBar\nBaz" "\nXX"]
XXfoo
XX
XXBar
XX
XXBaz
Where do the additonal newlines and identiation marks (X
) come from? Why does it look like I get two substitutions? Is this a bug, or is there a bit I do not understand about regular expression syntax?
For completnes sake here is the regex I use now puts [regsub -all -line "^" "foo\nBar\nBaz" "XX"]
-line
option in place of(?n)
->set t [regsub -all -line "^" $string "XX"]
. IMO that's more readable. – Pean-linestop
would suffice here, or(?w)
inline option,"(?w)^"
pattern.-line
or(?n)
also modify the behavior of.
and negated bracket exprrssions that are not used in the pattern. – Incandesce::textutil::adjust::indent foo\nBar\nBaz XX
or at leastjoin [lmap line [split foo\nBar\nBaz \n] {format {XX%s} $line}] \n
. – Dorotheadorothee