[Solved] Find a word after and before a string


that could be a really brief solution (=one of the many ones) to your problem, but the core concept would be something like that in every case.


the input has some random values:

let inputs = ["ab-0-myCoolApp.theAppAB.in", "ab-0-myCoolAppABX.theAppAB.in", "ab-0-myCoolAppABXC.theAppAB.in"]

and having a regular expression to find matches:

let regExp = try? NSRegularExpression(pattern: "-([^-]*?)\\.", options: NSRegularExpression.Options.caseInsensitive)

then Release the Kraken:

inputs.forEach { string in
    regExp?.matches(in: string, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, string.lengthOfBytes(using: .utf8))).forEach({
        let match = (string as NSString).substring(with: $0.range(at: 1))
        debugPrint(match)
    })
}

finally it prints out the following list:

"myCoolApp"
"myCoolAppABX"
"myCoolAppABXC"

NOTE: you may need to implement further failsafes during getting the matches or you can refactor the entire idea at your convenience.

2

solved Find a word after and before a string