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

发布于 2024-09-13 00:42:08 字数 115 浏览 5 评论 0原文

set phoneNumber 1234567890

这个数字是个位数,我想使用正则表达式将这个数字分成 123 456 7890。不使用 split 功能可以吗?

set phoneNumber 1234567890

this number single digit, i want divide this number into 123 456 7890 by using regexp. without using split function is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-09-20 00:42:08

以下代码片段:

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

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

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

(814) 465-8695

这使用捕获组在模式和 subMatchVar... for Tcl regexp

参考文献


关于模式

正则表达式模式是:

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

它有 3 个捕获组 (...)\d 是数字字符类的简写。此上下文中的 {3} 是“恰好 3 次重复”。

参考文献

The following snippet:

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

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

Prints (as seen on ideone.com):

(814) 465-8695

This uses capturing groups in the pattern and subMatchVar... for Tcl regexp

References


On the pattern

The regex pattern is:

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

It has 3 capturing groups (…). The \d is a shorthand for the digit character class. The {3} in this context is "exactly 3 repetition of".

References

清醇 2024-09-20 00:42:08
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";  

这是为 Perl 编写的,假设数字采用您指定的确切格式,希望这会有所帮助。

该网站可能会帮助您使用正则表达式
http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

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.

This site might help you with regex
http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文