import Foundation
let pattern = #"^"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
<?php
namespace App\Jobs\Library\Domain;
use Illuminate\Bus\Queueable;
use App\Services\YorLinkService;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class AddDomainLink implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected string $link_id;
protected string $domain_id;
protected string $path;
protected YorLinkService $yorLinkService;
/**
* Create a new job instance.
*
* @param $link_id
* @param $domain_id
* @param $path
*/
public function __construct(string $link_id, string $domain_id, string $path)
{
$this->yorLinkService = new YorLinkService;
$this->link_id = $link_id;
$this->domain_id = $domain_id;
$this->path = $path;
}
/**
* @param $yorLinkService
*/
public function setYorLinkServiceMock($yorLinkService)
{
$this->yorLinkService = $yorLinkService;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->yorLinkService->createLink([
'link_id' => $this->link_id,
'domain_id' => $this->domain_id,
'path' => $this->path
]);
}
}
"""#
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