mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 20:10:08 +02:00
45 lines
1016 B
Swift
45 lines
1016 B
Swift
//
|
|
// VersionExtractor.swift
|
|
// PHP Monitor
|
|
//
|
|
// Created by Nico Verbruggen on 16/12/2021.
|
|
// Copyright © 2022 Nico Verbruggen. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class VersionExtractor {
|
|
|
|
/**
|
|
This attempts to extract the version number from any given string.
|
|
*/
|
|
public static func from(_ string: String) -> String? {
|
|
do {
|
|
let regex = try NSRegularExpression(
|
|
pattern: #"(?<version>(\d+)(.)(\d+)((.)(\d+))?)"#,
|
|
options: []
|
|
)
|
|
|
|
let match = regex.matches(
|
|
in: string,
|
|
options: [],
|
|
range: NSRange(location: 0, length: string.count)
|
|
).first
|
|
|
|
guard let match = match else {
|
|
return nil
|
|
}
|
|
|
|
let range = Range(
|
|
match.range(withName: "version"),
|
|
in: string
|
|
)!
|
|
|
|
return String(string[range])
|
|
} catch {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
}
|