import Foundation
let pattern = #"translate\(\'([^)]*)\',"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
<div class="row">
<div class="col-md-12">
<div class="content-title"><?php echo $this->translate('Contattaci'); ?></div>
<?php if ( $this->messageProcessed && $this->sendError ): ?>
<div class="alert alert-danger">
<h4><span class="glyphicon glyphicon-exclamation-sign"></span> <?php echo $this->translate('Errore Invio Messaggio'); ?></h4>
<?php echo $this->translate('Spiacenti, si é verificato un errore durante l’invio del messaggio'); ?>.
<p class="error"><?php echo $this->sendError; ?></p>
</div>
<?php elseif ( $this->messageProcessed & !$this->sendError ): ?>
<div class="alert alert-success">
<h4><span class="glyphicon glyphicon-ok-circle"></span> <?php echo $this->translate('Messaggio Inviato'); ?></h4>
<?php echo $this->translate('Grazie per il tuo messaggio. Risponderemo nel piú breve tempo possibile'); ?>.
</div>
<?php else: ?>
<div class="well well-sm">
<?php echo $this->translate('Compila il modulo seguente per inviarci un tuo messaggio.'); ?><br />
<?php echo $this->translate('Sarà nostra premura rispondere nel minor tempo possibile per fornirti tutte le informazioni di cui puoi necessitare.'); ?>
</div>
<?php if ( $this->getSetting('site_phone') ): ?>
<div class="alert alert-info alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<span class="glyphicon glyphicon-info-sign"></span> <?php echo $this->translate('Se preferisci, puoi contattarci telefonicamente al numero <b>%s</b>', $this->getSetting('site_phone')); ?>
</div>
<?php endif; ?>
<div class="contact-form">
<?php echo $this->form; ?>
<div class="clearfix"></div>
<p> </p>
<div class="small text-justify well well-sm">
<?php echo $this->translate('I dati personali saranno trattati ai sensi del D.Lgs. 196/2003, sulla tutela delle persone e di altri soggetti rispetto al trattamento dei dati personali, il trattamento delle informazioni che ti riguardano, sarà improntato ai principi di correttezza, liceità e trasparenza e tutelando la tua riservatezza e i tuoi diritti.'); ?>
</div>
</div>
<?php endif; ?>
</div>
</div>
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression