Regular expressions: Difference between revisions
Content added Content deleted
m (→{{header|AutoHotkey}}: Minor indentation and casing edit) |
(→{{header|Tcl}}: demonstrate substring extraction as well, and apply a bunch of corrections) |
||
Line 488: | Line 488: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Test using < |
Test using <code>regexp</code>: |
||
<lang tcl>set theString "I am a string" |
<lang tcl>set theString "I am a string" |
||
if {[regexp -- {string$} $theString]} { |
if {[regexp -- {string$} $theString]} { |
||
puts "Ends with 'string' |
puts "Ends with 'string'" |
||
} |
} |
||
if {![regexp -- {^You} $theString]} { |
if {![regexp -- {^You} $theString]} { |
||
puts "Does not start with 'You' |
puts "Does not start with 'You'" |
||
}</lang> |
}</lang> |
||
Extract substring using <code>regexp</code> |
|||
<lang tcl>set theString "This string has >123< a number in it" |
|||
if {[regexp -- {>(\d+)<} $theString -> number]} { |
|||
puts "Contains the number $number" |
|||
}</lang> |
|||
Substitute using <code>regsub</code> |
|||
<lang tcl>set theString = "I am a string" |
<lang tcl>set theString = "I am a string" |
||
puts [regsub -- { +a +} $theString { another }]</lang> |
puts [regsub -- { +a +} $theString { another }]</lang> |