Mallikarjunarao Kosuri
Mallikarjunarao Kosuri

Reputation: 1111

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. without using split function is it possible?

Upvotes: 2

Views: 1597

Answers (2)

polygenelubricants
polygenelubricants

Reputation: 383866

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

Upvotes: 7

Graeme Smyth
Graeme Smyth

Reputation: 136

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

Upvotes: 0

Related Questions