Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between [0-9] and \d in tcl

Tags:

regex

tcl

I tried

puts [regexp "(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})" 192.168.1.10]

answer is 1.

But if I use

puts [regexp "(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})" 192.168.1.10]

(replaced \[0-9] with \d) answer is 0.

Can someone please tell me why?

like image 638
Ram Avatar asked Dec 05 '25 10:12

Ram


1 Answers

You should either escape the backslash in the \d as \\d when used with double quotes.

puts [regexp "(\\d{1,3})\.(\\d{1,3})\.(\\d{1,3})\.(\\d{1,3})" 192.168.1.10]

Or you have to use braces.

puts [regexp {(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})} 192.168.1.10]

Note : With braces, variable substitution won't happen.

like image 53
Dinesh Avatar answered Dec 07 '25 00:12

Dinesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!