Code: Select all
scan $botname %\[^!\]!%\[^@\]@%s nick user host
Code: Select all
regexp {(.*)!(.*)@(.*)} $botname match nick user host
Code: Select all
regexp -all {A} $string
Code: Select all
regexp -all {the} $string
Code: Select all
regexp -all {[abcd]} $string
#This code will count and add all the number of a's, b's, c's and d's found
Code: Select all
regexp -all {[^abcd]} $string
#This code will check and add all the number of a's, b's, c's and d's found. #The total number should be 0, for this statement to be true. (negative logic)
Code: Select all
regexp "string" $string
regexp \[string\] $string
regexp {string} $string
Code: Select all
#To count the number of ['s or use:
regexp -all \[\\\[\] $string
#To count the number of {'s or use:
regexp -all \[\\\\\] $string
#To count the number of {'s or use:
regexp -all \[{\] $string
NOTE: Generally you will only need to add 3 escape's infront of each [, ] or \ special characters. For others mostly you need not.
Code: Select all
regexp -nocase -all {abc} $string
#and
regexp -nocase -all {ABC} $string
#will be considered the same then
Code: Select all
#To match a character in between the range of a, b, c, d, ..........z:
regexp {[a-z]} $string > will give 1 for MATCH, 0 for NO-MATCH (lower case match)
regexp -all {[a-z]} $string > will return total number of MATCHES (lower case match)
regexp -nocase -all {[a-z]} $string > will return total number of MATCHES (case ignored)
#To match a character in between the range of 0, 1, 2, 3, ..........9:
regexp {[0-9]} $string > will give 1 for MATCH, 0 for NO-MATCH
regexp -all {[0-9]} $string > will return total number of MATCHES
#Note: The nocase switch for the [0-9] would be redundant.
#To match a character in between the range of a, b, c,....z and 0, 1, 2...9:
regexp {[a-z0-9]} $string > will give 1 for MATCH, 0 for NO-MATCH
regexp -all {[a-z0-9]} $string > will return total number of MATCHES
Code: Select all
regexp {^string_here$} $string
^ = Assert position at the start of the string
$ = Assert position at the end of the string (or before the line break)
$+ = Assert position at the end of the string (or before the line break)
Code: Select all
regexp {abc|efg|hij} $string
#This will try to match "abc" or "efg" or "hij" if none is found, returns 0, if anyone is found returns 1.
Code: Select all
regexp {^[^abcd]$} $string
#If a, b, c and d are not present return 1, if anyone of them is, return 0.
Code: Select all
regexp {^[a-z]{3,}[0-9]{2,}$} $string
#This will match only if 3 or more characters are present in the range [a-z] and 2 or more characters in the range [0-9] of the string.
#Example:
abgfg452 > will match
as456342 > will not match
abc12 > will match
Code: Select all
regexp {^[a-z]{3,5}[0-9]{2,8}$} $string
#This will match only if 3, 4 or 5 characters are present in the range [a-z] and 2, 3, 4, 5, 6, 7 or 8 characters in the range [0-9] of the string.
#Examples:
adfsd3463 > will match
adsfsdgfs325 > will not match
wer436234 > will match
gdtweer436322512 > will not match
Code: Select all
regexp {^[a-z]{4}[0-9]{3}$} $string
#This will match only if 4 characters are present in the range [a-z] and 3 characters in the range [0-9] of the string.
#Examples:
wrew364 > will match
we436 > will not match
whg6743 > wil not match
wga63 > will not match
se65 > will not match
Code: Select all
regexp {https?} $string
This will return 1 if "http" or "https" is present in the string, else return 0.
Code: Select all
regexp {([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})} $string
#IP Address -- Matches 0.0.0.0 through 999.999.999.999
regexp {(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $string
#IP Address -- Matches 0.0.0.0 through 255.255.255.255
regexp {(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]}
#Matching a url
regexp {[0-9]{5}(?:-[0-9]{4})?}
#US Zipcode
regexp {[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}}
#Email address
regexp {(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}}
#Date in formats: dd-mm-yy, dd.mm.yy, dd/mm/yy
regexp {^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$}
#Matching all major credit cards
Code: Select all
#regsub
regsub -all {a} $data "" data
#Will remove all occurences of character "a" in the string $data.
regsub -all {a} $data "b" data
#Will replace all occurences of character "a" in the string by "b".
Similarly,
#string map
string map {"a" ""} $data
#Will remove all occurences of character "a" in the string $data.
#string map
string map {a b} $data
string map {"a" "b"} $data
#Will replace all occurences of character "a" in the string by "b".
Code: Select all
#This will remove all occurences of a, b, c and d in the string $data.
regsub -all {[abcd]} "sgfdszasbdgds" "" data
#Note: We are using the -all switch here so it will return '5' as per the matches.
You can also strip control codes (colors, bolds, underlines etc) from strings using regsub, string map filters as you might have seen in most posts on the forum. Here are some I found on the forum:
#For removing colors
regsub -all {\003([0-9]{1,2}(,[0-9]{1,2})?)?} $str "" str
#For removing control codes
regsub -all {\017|\037|\002|\026|\006|\007} $str "" str
#For removing control codes
set str [string map {"\017" "" "\037" "" "\002" "" "\026" "" "\006" "" "\007" ""} $str]
Code: Select all
#regsub
proc filter {data} {
regsub -all -- \\\\ $data \\\\\\\\ data
regsub -all -- \\\[ $data \\\\\[ data
regsub -all -- \\\] $data \\\\\] data
regsub -all -- \\\} $data \\\\\} data
regsub -all -- \\\{ $data \\\\\{ data
regsub -all -- \\\$ $data \\\\\$ data
regsub -all -- \\\" $data \\\\\" data
return $data
}
#Taken from: http://www.peterre.com/characters.html
#string map
proc filter {data} {
set data [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\} $ \\\$ \" \\\"} $data]
}
#Taken from: spambuster.tcl
Code: Select all
\, [, ], {, }, $, "
Code: Select all
regsub -all "\002|\003|\017|\026|\037" $text "" text
regsub -all {\002|\003|\017|\026|\037} $text "" text
Code: Select all
#To remove the total number of capital letters in a string:
regsub -all {[A-Z]} $text "" counted
#The total number of capital letters in $text will be placed in $counted and $text would have been stripped of the capital letters.
Same goes similarly with numbers, [0-9] or both [a-z0-9].
Also the -nocase switch is available in regsub for case sensitive matching or if you want to ignore cases while matching -- only for alphabets.
Code: Select all
regsub -all {a} $text "" counted
#is similar as:
set counted 0
for {set count 0} {$count < [string length $text]} {incr text} {
if {[string equal "a" [string index $text $count]]} {
incr counted
}
}
(Adv: As you can see regsub is more simpler, easier and has a smaller code)
(Disadv: regsub is slower than string map)
Code: Select all
% set str "<tag>foo</tag>some text<tag>bar</tag>"
% regsub -all {<tag>(.*)</tag>} $str ""
%
Code: Select all
% set str "<tag>foo</tag>some text<tag>bar</tag>"
% regsub -all {<tag>(.*?)</tag>} $str ""
some text
%