$re = '/dn: \t"CN=(.*?),OU=(.*?),DC=(PACRIM1|global|honeywell|namerica1|namerica2|namerica3|namerica4),.*\t\tdescription:\t\t(.*)\tdisplayName: \t\t(.*)\t\thwObjectGUID: \t\t(.*)\t\textensionAttribute7: \t\t(.*)\t\tmail: \t\t(.*)\t\tobjectClass: \t\t(.*)/';
$str = 'dn: "CN=DL - Wipro CIM Team,OU=Distribution Lists,OU=Exchange,OU=US,DC=global,DC=ds,DC=honeywell,DC=com" hwDomain: description: !e329288 - INC000009225560 displayName: DL - Wipro CIM Team hwObjectGUID: b347d344-5d04-4d40-b053-61662ff20d45 extensionAttribute7: E130111 mail: DL-WiproCIMTeam@Honeywell.com objectClass: group
dn: "CN=P3-Sales-Mktng-Europe (BI35),OU=New Objects,OU=HMX-BI35,OU=Honeywell,OU=From Aelita,OU=Exchange,DC=global,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: P3-Sales-Mktng-Europe (BI35) hwObjectGUID: 3f042f02-cf8a-4357-8e82-4a9c110d8834 extensionAttribute7: E155937 mail: HMX-SalesMktng-Europe@Honeywell.com objectClass: group
dn: "CN=ISLC Focal Point - Philippines (AS),OU=Distribution Lists,OU=Exchange,OU=APAC,DC=global,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: ISLC Focal Point - Philippines (AS) hwObjectGUID: d26b7d04-3b2a-411e-9461-05f24459d6b4 extensionAttribute7: mail: ISLCFocalPoint-PhilippinesAS@Honeywell.com objectClass: group
dn: "CN=DL_PEHA_Engg,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: !H125516 #INC000008689478 displayName: DL_HTS_BCS_EMEA_LC_PEHA_Engg hwObjectGUID: 9dfe2963-3309-4285-8eb0-f1f23f08dce1 extensionAttribute7: H125516 mail: DL_HTS_BCS_EMEA_LC_PEHA_Engg@honeywell.com objectClass: group
dn: "CN=DL_ECBI_Engg,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: !H125516 #INC000008689478 displayName: DL_ECBI_Engg hwObjectGUID: 1ed1f115-427a-4ed7-ad59-992e71a9c011 extensionAttribute7: H125516 mail: DL_ECBI_Engg@honeywell.com objectClass: group
dn: "CN=DL-GCCtransitionproject,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-GCCtransitionproject hwObjectGUID: 20d9e78b-bead-4a66-add6-504e43084b54 extensionAttribute7: !e596177 mail: DL-GCCtransitionproject@Honeywell.com objectClass: group
dn: "CN=DL _Non Spares&ERB,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL _Non Spares&ERB hwObjectGUID: fe7bc87d-eaa7-43fe-85cd-9e0df6e42bb5 extensionAttribute7: E553700 mail: DL_NonSparesERB@Honeywell.com objectClass: group
dn: "CN=DL-India Staffing Leaders,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: E855373-#INC000008575463 displayName: DL-India Staffing Leaders hwObjectGUID: 2cf5a77e-b6e8-49f9-9e1f-149a96f304a7 extensionAttribute7: !E580316 mail: DL-India-Staffing-Leaders@Honeywell.com objectClass: group
dn: "CN=DL-HLS 3S SUITE-QA Team,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-HLS 3S SUITE-QA Team hwObjectGUID: 9293982e-de2a-47ec-84fe-95d45ed5d8ef extensionAttribute7: mail: DL-HLS3SSUITE-QATeam@Honeywell.com objectClass: group
dn: "CN=DL-HPS_EFS_HTS_HYD,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-HPS_EFS_HTS_HYD hwObjectGUID: dfccf1e2-1124-443a-8a0c-b1caf67ea4af extensionAttribute7: mail: RS90329@Honeywell.com objectClass: group
dn: "CN=DL CA&ESSProblemcoordinators,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL CA&ESSProblemcoordinators hwObjectGUID: d8ad06bf-0d40-483b-80f4-3073fd181f66 extensionAttribute7: E283778 mail: DLCA&ESSProblemcoordinators@Honeywell.com objectClass: group
dn: "CN=DL-EOPS ALL PM,OU=IN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: displayName: DL-EOPS ALL PM hwObjectGUID: 21c834ca-ba76-4b84-a4d4-0d1e99223412 extensionAttribute7: E159431 mail: DL-EOPSALLPM@Honeywell.com objectClass: group
dn: "CN=DL-Fire China Engineering,OU=CN,DC=PACRIM1,DC=ds,DC=honeywell,DC=com" hwDomain: description: !E506546 - INC000009116231 displayName: DL-Fire China Engineering hwObjectGUID: 0bd5c412-9fb5-4acb-a060-b40006957674 extensionAttribute7: E227848 mail: DL-FireChinaEngineering@Honeywell.com objectClass: group
';
$subst = "dn:cn=$1,ou=$3,ou=Groups,o=honeywell\ncn: $1\nhwDomain: $3\ndescription: $4\ndisplayName: $5\nhwObjectGUID: $6\nowner: $7\nmail: $8\nobjectClass: $9\n";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php