简体   繁体   中英

Find and Replace regex in Xcode: How to append Round brackets before first closing Round brackets using regular expression?

Recently in my project make one enum as an associated type. It's used in thousands of places. I want to append Round brackets() when accessing this enum in the whole project. I am trying to use Xcode's find and replace feature. For that, I wrote regular expressions. But it's not properly working.

struct Model {}

enum Investment {
    case stocks(Stocks)
}

enum Stocks {
    case highRisk(model: Model? = nil)
    case lowRisk(model: Model? = nil)
    case veryHighRisk(model: Model? = nil)
}

class Test {
    func configStockUI(model: Investment, userName: String? = nil) {
    }
}

func renderUI() {
    let test = Test()
    test.configStockUI(model: .stocks(.highRisk), userName: "Deepak")
    test.configStockUI(model: .stocks(.lowRisk))
    test.configStockUI(model: .stocks(.veryHighRisk))

    //Expected Output -
    test.configStockUI(model: .stocks(.highRisk()), userName: "Deepak")
    test.configStockUI(model: .stocks(.lowRisk()))
    test.configStockUI(model: .stocks(.veryHighRisk()))

    //Actual output -
    test.configStockUI(model: .stocks(.highRisk), userName: "Deepak"())
    test.configStockUI(model: .stocks(.lowRisk)())
    test.configStockUI(model: .stocks(.veryHighRisk)())
}

My Regular expressions to find desired result - .stocks\(.(.*)\)

to replace - .stocks\(.$1()\)

[ same1 的 POC Xcode 截图

In the below regex I tried to match the enum cases as close as possible to avoid false positives and made use of groups for easier replacing

Find Regex:

(\.stocks\(.)(high|low|veryHigh)(Risk)\)

Replace pattern

$1$2$3())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM