import Foundation
let pattern = #"\[serviceOrderId:(?<oc_request_id>.[^]]*)\]"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = ##"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div><font face="Arial" color="#000000" size="2">***** Reply to this email to append information to [[WO#17561]] *****<br><br>WO# 17561 was amended to include the following information:<br><br>Thank you for contacting us.<br><br><br>We have received your request and will respond within 2 business days.<br><br><br>Your request number is: REQ-20665.<br><br><br>If you would like to speak with a representative, please contact us at 1 855 242-4492 and reference your request number.<br><br><br>Our hours of operation are Monday to Friday, 8:00 am to 5:00 pm AST.<br><br><br>Thank you for your continued business with Bell Aliant.<br><br><br>Client Services<br>Bell Business Markets<br><br><br><br><br><br><br><br>Legal | Privacy | bell.ca <br><br>Corporate Secretary’s Office of Bell Canada, Bell TV, Bell Mobility, Bell Media and Bell Aliant<br>1 carrefour Alexander-Graham-Bell, Building A-7, Verdun, Quebec, H3E 3B3 <br><br><br><br>© Bell Canada, 2020. All rights reserved.<br><br><br><br><br>[serviceOrderId:bsd_REQ-20665] <br><br>Merci de nous avoir contacté.<br><br><br>Nous avons bien reçu votre demande et nous y réponderons dans 2 jours ouvrables.<br><br><br>Votre numéro de demande est : REQ-20665.<br><br><br>Si vous souhaitez parler à un conseiller, veuillez composer le 1 855 242-4492 et faire référence à votre numéro de demande.<br><br><br>Nos heures d'ouverture sont de 8 h à 17 h AST, du lundi au vendredi.<br><br><br>Merci de votre fidélité envers Bell Aliant.<br><br><br>Services clients<br>Bell Marché Affaires<br><br><br><br><br><br><br><br>Avis juridiques | Sécurité | bell.ca <br><br>Bureau du secrétaire de Bell Canada, Bell Télé, Bell Mobilité, Bell Média et Bell Aliant<br>1, carrefour Alexander-Graham-Bell, Aile A-7, Verdun (Québec) H3E 3B3 <br><br><br><br>© Bell Canada, 2020. Tous droits réservés.<br><br><br><br><br>[serviceOrderId:bsd_REQ-20665]<br><br></font></div><hr> <span style="font-size: 12px;"><font color="blue"><em><strong>External Email:</strong> Please use caution when opening links and attachments / </em></font></span><em> <span style="font-size: 12px;"><font color="blue"><strong>Courriel externe:</strong> Soyez prudent avec les liens et documents joints </font></span></em></body></html>"##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression