使用正则表达式或其他方法在字符串中的模式中查找字符串模式 Swift

Find string pattern within pattern in string using regex or other method Swift

我正在寻找一种方法来识别字符模式中的字符模式并删除该模式的任何内部示例(如果存在)以仅保留外部示例。

例如:

str = "some text [outer part [inner part] back to outer part] more text"

我要删除内图[ ]离开:

str = "some text [outer part inner part back to outer part] more text"

这并不总是格式。还可以看到:

 str = "this text [does does no need inner brackets removed] as there aren't any"

 str = "this text [does does not]  need inner brackets [removed] as there aren't any"

 str = "this text [has one [instance] of inner brackets] and another [that is] okay"

注意:如果打开和关闭分隔符不同是一个问题,我可以将它们更改为一个分隔符,例如 * 但我仍然想去掉内部分隔符。

这看起来很简单,但事实证明比我预期的要难,因为 str_replace 不能自然地检测出哪个是外部的,哪个是内部的。例如,在下面我可以找到字符 [ 但不确定如何只删除它是否在另一个 [...

let string = "some text [outer part [inner part] back to outer part] more text"

if string.range(of: "[\b(?=.*[)[a-zA-Z]{1,8}\b", options: [.regularExpression, caseInsensitive]) != nil {
print("found a match")
} else {
print("no match present")
}

感谢您的任何建议。

像这样的事情怎么样:

func removeInnerDelimiters(S: String) -> String {
    var S = S
    var lastOpeningCharPos = -1
    var closingCharPos = -1
    for (index, c) in S.enumerated() {
        if c == "[" {
            lastOpeningCharPos = index
        } else if c == "]" {
            closingCharPos = index
            break
        }
    }
    if lastOpeningCharPos > -1 && closingCharPos > 0 {
        S.remove(at: S.index(S.startIndex, offsetBy: closingCharPos))
        S.remove(at: S.index(S.startIndex, offsetBy: lastOpeningCharPos))
    }
    return S
}

所以基本上你遍历整个字符串,当你找到第一个 ] 时,你只需删除你找到的最后一个 [,当然还有 ]

您可以找到右括号的第一个索引,然后搜索左括号的最后一个索引,直到该索引为止。然后你可以检查前后的子字符串是否有左括号和右括号:

extension StringProtocol where Self: RangeReplaceableCollection {
    mutating func removeInnerBrackets() {
        if let close = firstIndex(of: "]"),
            let open = self[..<close].lastIndex(of: "["),
            let _ = self[..<open].firstIndex(of: "["),
            let _ =  self[index(after: close)...].firstIndex(of: "]") {
            remove(at: close)
            remove(at: open)
        }
    }
}

var sentence = "some text [outer [part [inner part] back to outer] part] more text"
sentence.removeInnerBrackets()
sentence // "some text [outer [part inner part back to outer] part] more text"