use strict;
my $str = '######################
# Must Match:
######################
Social Security Number
Social Security Num
Social Security No
Social Security #
Social Security#
Social Security
Social Sec Number
Social Sec Num
Social Sec No
Social Sec #
Social Sec#
Social Sec
Social Sec. Number
Social Sec. Num
Social Sec. No
Social Sec. #
Social Sec.#
Social Sec.
Soc. Sec Number
Soc. Sec Num
Soc. Sec No
Soc. Sec #
Soc. Sec#
Soc. Sec
Soc. Sec. Number
Soc. Sec. Num
Soc. Sec. No
Soc. Sec. #
Soc. Sec.#
Soc. Sec.
Social Insurance Number
Social Insurance Num
Social Insurance No
Social Insurance #
Social Insurance#
SS Num
S.S Num
S.S. Num
SSN#
S.S.N#
S.S.N.#
SSN
S.S.N
S.S.N.
SS #
S.S #
S.S. #
SS#
S.S#
S.S.#
SS ID
SS I.D
SS I.D.
S.S. ID
S.S.I.D.
S.S. I.D
S.S. I.D.
SSID
S.S.I.D
S.S.I.D.
SSN123-
SSN 123
SSN# 123
SSN. 1234
SSN.123
# In context, lowercase:
The following is a list of social insurance numbers:
Yo, get d33z l33t ss# and profit!
######################
# Must Not Match:
######################
# More than social:
Social
Social Number
Social Justice
# No disallowed suffix:
Social Security Administration
S.S. Admin
SS Admin
Social Security Office
S Sec Office
Social Security Tax
# Social required:
Security ID
Security Number
# Invalid combination of security/insurance:
Soc Sec Insurance No
Social Insurance Sec No
';
my $regex = qr/(?i)\bS(?:ocial|oc\.?|\.)? ?(?!(?:S(?:ecurity|ec\.?|\.)?|Insurance) ?(?:Admin|Insurance|Office|Sec|Tax))(?:(?:S(?:ecurity|ec\.?|\.)?)|(?:Insurance)) ?(?:N(?:\.|o\.?|umbers?|um\.?)?|I\.?D)?(?:\d|#|\.|\b)/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html