简体   繁体   English

如何使用正则表达式提取数字中的单个数字

[英]how to extract a single digit in a number using regexp

set phoneNumber 1234567890

this number single digit, i want divide this number into 123 456 7890 by using regexp. 这个数字个位数,我想通过使用正则表达式将此数字分为123 456 7890。 without using split function is it possible? 不使用拆分功能,这可能吗?

The following snippet: 以下代码段:

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

puts "($areacode) $first-$second"

Prints ( as seen on ideone.com ): 打印( 如ideone.com所示 ):

(814) 465-8695

This uses capturing groups in the pattern and subMatchVar... for Tcl regexp 这将在模式和subMatchVar...使用捕获组 subMatchVar...用于Tcl正则regexp

References 参考


On the pattern 在模式上

The regex pattern is: 正则表达式模式为:

(\d{3})(\d{3})(\d{4})
\_____/\_____/\_____/
   1      2      3

It has 3 capturing groups (…) . 它具有3个捕获组(…) The \\d is a shorthand for the digit character class. \\d是数字字符类的简写。 The {3} in this context is "exactly 3 repetition of". 在这种情况下, {3}是“的3个重复”。

References 参考

my($number) = "8144658695";

$number =~ m/(\d\d\d)(\d\d\d)(\d\d\d\d)/;

my $num1 = $1;
my $num2 = $2;
my $num3 = $3;

print $num1 . "\n";
print $num2 . "\n"; 
print $num3 . "\n";  

This is writen for Perl and works assuming the number is in the exact format you specified, hope this helps. 这是为Perl写的,并且在假定数字与您指定的格式完全相同的情况下可以正常工作,希望能有所帮助。

This site might help you with regex http://www.troubleshooters.com/codecorn/littperl/perlreg.htm 该网站可能会帮助您使用正则表达式http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM