656-45-7752
555-43-5743
666-44-7770
567-66-4321
567-44-4321
CODE FOLLOWS Tony PHP Example

Tony's REGEX Problem

Extract the Social Security numbers from the following text. In other words, create a REGEX pattern to gather the following SS numbers and nothing else.

Social Security Numbers:

656-45-7752
555-43-5743
666-44-7770
567-66-4321
567-44-4321


REGEX CODE FOLLOWS

<?php
// stores sample text (names, phones, SS, Dr Lic, Zip Code)
$string '
Name: Sam Parker
Phone: 517-333-1345
SS: 656-45-7752
Dr Lic: 345-678-123-456
Zip Code: 48910-3465

Name: Mary Parker
Phone: 517.333.1334
SS: 555-43-5743
Dr Lic: 333.478.646.233
Zip Code: 48910.3464

Name: Joe Spade
Phone: 517/289/0453
SS: 666-44-7770
Dr Lic: 123/456/789/001
Zip Code: 48910

Name: Matt Buds
Phone: 517 393 1556
SS: 567-66-4321
Dr Lic: 123 654 987 002
Zip Code: 48910 3457

Name: Karen Klutz
Phone: 517-393.1446
SS: 567-44-4321
Dr Lic: 123/654-987.002
Zip Code: 48910 3457'
;

// pattern to find Social Security numbers after SS:
$pattern "/SS:\s*([0-9]{3}-[0-9]{2}-[0-9]{4})/";

// find all matching SS numbers
preg_match_all($pattern$string$matches);

echo(
'<pre>');
foreach (
$matches[1] as $ssn) {
    echo 
$ssn "\n";
}
echo(
'</pre>');
?>