How do I extract all matches with a Tcl regex?

Mallikarjunarao Kosuri picture Mallikarjunarao Kosuri · Jul 30, 2010 · Viewed 29.5k times · Source

hi everybody i want solution for this regular expression, my problem is Extract all the hex numbers in the form H'xxxx, i used this regexp but i didn't get all hexvalues only i get one number, how to get whole hex number from this string

set hex "V5CCH,IA=H'22EF&H'2354&H'4BD4&H'4C4B&H'4D52&H'4DC9"
set res [regexp -all {H'([0-9A-Z]+)&} $hex match hexValues]
puts "$res H$hexValues"

i am getting output is 5 H4D52

Answer

polygenelubricants picture polygenelubricants · Jul 30, 2010

On -all -inline

From the documentation:

-all : Causes the regular expression to be matched as many times as possible in the string, returning the total number of matches found. If this is specified with match variables, they will contain information for the last match only.

-inline : Causes the command to return, as a list, the data that would otherwise be placed in match variables. When using -inline, match variables may not be specified. If used with -all, the list will be concatenated at each iteration, such that a flat list is always returned. For each match iteration, the command will append the overall match data, plus one element for each subexpression in the regular expression.

Thus to return all matches --including captures by groups-- as a flat list in Tcl, you can write:

set matchTuples [regexp -all -inline $pattern $text]

If the pattern has groups 0…N-1, then each match is an N-tuple in the list. Thus the number of actual matches is the length of this list divided by N. You can then use foreach with N variables to iterate over each tuple of the list.

If N = 2 for example, you have:

set numMatches [expr {[llength $matchTuples] / 2}]

foreach {group0 group1} $matchTuples {
   ...
}

References


Sample code

Here's a solution for this specific problem, annotated with output as comments (see also on ideone.com):

set text "V5CCH,IA=H'22EF&H'2354&H'4BD4&H'4C4B&H'4D52&H'4DC9"
set pattern {H'([0-9A-F]{4})}
 
set matchTuples [regexp -all -inline $pattern $text]
 
puts $matchTuples
# H'22EF 22EF H'2354 2354 H'4BD4 4BD4 H'4C4B 4C4B H'4D52 4D52 H'4DC9 4DC9
# \_________/ \_________/ \_________/ \_________/ \_________/ \_________/
#  1st match   2nd match   3rd match   4th match   5th match   6th match
 
puts [llength $matchTuples]
# 12
 
set numMatches [expr {[llength $matchTuples] / 2}]
puts $numMatches
# 6
 
foreach {whole hex} $matchTuples {
   puts $hex
}
# 22EF
# 2354
# 4BD4
# 4C4B
# 4D52
# 4DC9

On the pattern

Note that I've changed the pattern slightly:

  • Instead of [0-9A-Z]+, e.g. [0-9A-F]{4} is more specific for matching exactly 4 hexadecimal digits
  • If you insist on matching the &, then the last hex string (H'4DC9 in your input) can not be matched
    • This explains why you get 4D52 in the original script, because that's the last match with &
    • Maybe get rid of the &, or use (&|$) instead, i.e. a & or the end of the string $.

References