如何使用regexp提取数字中的单个数字

Mal*_*uri 2 regex tcl

set phoneNumber 1234567890
Run Code Online (Sandbox Code Playgroud)

这个数字的单个数字,我想用regexp将这个数字分成123 456 7890.不使用拆分功能有可能吗?

pol*_*nts 7

以下片段:

regexp {(\d{3})(\d{3})(\d{4})} "8144658695" -> areacode first second

puts "($areacode) $first-$second"
Run Code Online (Sandbox Code Playgroud)

打印(如ideone.com上所示):

(814) 465-8695
Run Code Online (Sandbox Code Playgroud)

这使用模式中的捕获组subMatchVar...Tclregexp

参考


在模式上

正则表达式模式是:

(\d{3})(\d{3})(\d{4})
\_____/\_____/\_____/
   1      2      3
Run Code Online (Sandbox Code Playgroud)

它有3个捕获组(…).这\d是数字字符类的简写.该{3}在这方面是"刚好3的重复".

参考

  • 对于regexp,使用" - >"虚拟变量名称是相当常见的,因为它1)表明你没有使用那些信息,2)只是看起来很好(即,有点读取为"进入"). (2认同)