Perhaps this regex will meet your needs:
\b(\d+)((?:\.\d+)+)\b
This regex separates the leading integer sequence from its repeating fractional components.
(As indicated by @ysth, please keep in mind that \d
may match more characters than you intend. If that is the case, use the character class [0-9]
instead or use the /a
modifier.)
Here's a Perl program that demonstrates this regex on a sample data set. (Also see the live demo.)
#!/usr/bin/perl -w
use strict;
use warnings;
while (<DATA>) {
chomp;
# A - A sequence of digits
# B - A period and a sequence of digits
# C - Repeat 'B'.
if (/\b(\d+)((?:\.\d+)+)\b/) {
# ^^^ ^^^^^
# A B
# ^^^^^^^
# C
print "[$1] [$2]\n";
}
}
__END__
1.23
123.456
1.2.3
1.22.333.444
Expected Output:
[1] [.23]
[123] [.456]
[3] [.2.3]
[4] [.22.333.444]