I need a function in Swift 3 which will remove content in a string between parentheses.
E.g for a string like "THIS IS AN EXAMPLE (TO REMOVE)"
should return "THIS IS AN EXAMPLE"
I'm trying to use removeSubrange
method but I'm stuck.
Most simple Shortest solution is regular expression:
let string = "This () is a test string (with parentheses)"
let trimmedString = string.replacingOccurrences(of: #"\s?\([\w\s]*\)"#, with: "", options: .regularExpression)
The pattern searches for:
\s?
.\(
.[\w\s]*
.\)
.Alternative pattern is #"\s?\([^)]*\)"#
which represents:
\s?
.\(
.[^)]*
.\)
.