How to extract all matches with a Tcl regex?

Hi everyone, I want a solution for this regular expression, my problem is: Extract all the hexadecimal numbers in the form H'xxxx, I used this regular expression, but I did not get all the hexadecimal values, only I got one number, how to get the integer hexadecimal 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 get pin 5 H4D52

+5
source share
2 answers

On -all -inline

From the documentation :

-all: , . , .

-inline: , . -inline . -all, , . , .

, - - Tcl, :

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

0…N-1, N -tuple . , - , N. foreach N .

N = 2 , :

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

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


, (. 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

, :

  • [0-9A-Z]+, . [0-9A-F]{4} 4
  • &, (H'4DC9 )
    • , 4D52 script, &
    • , & (&|$), a & $.

+20

Tclish, , -inline -all:

regexp -all -inline {H'([0-9A-Z]+)&} $string

EDIT: (. ):

regexp -all -inline {H'[0-9A-F]+&} $string
+2

All Articles