Jump to content

Regular expressions: Difference between revisions

→‎{{header|Tcl}}: demonstrate substring extraction as well, and apply a bunch of corrections
m (→‎{{header|AutoHotkey}}: Minor indentation and casing edit)
(→‎{{header|Tcl}}: demonstrate substring extraction as well, and apply a bunch of corrections)
Line 488:
 
=={{header|Tcl}}==
Test using <ttcode>regexp</ttcode>:
<lang tcl>set theString "I am a string"
if {[regexp -- {string$} $theString]} {
puts "Ends with 'string'\n"
}
if {![regexp -- {^You} $theString]} {
puts "Does not start with 'You'\n"
}</lang>
 
SubstituteExtract substring using <ttcode>regsubregexp</ttcode>
<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"
puts [regsub -- { +a +} $theString { another }]</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.