2

Is it possible to search string within another string using wildcards ? I want to find occurrence of ""text1" = "text2"" where text1 and text2 can be any string.

My initial string is

"This is my string. This string includes "text1" = "text2" and much more"

I don't know text1 and text2 when searching. I thought about something like """ = """ but no results.

EDIT: Let my try to explain on other example. I have *.swift file with a couple occurrences of locX extension

        labelPatternForExpresion.stringValue = "labelPatternForExpresion".locX(withComment: "comment one")
    labelPath.stringValue = "labelPathToProject".locX(withComment: "comment six")
    labelHeader.stringValue = "labelFileHeader".locX(withComment: "no comment")
    btnFromFile.title = "btnFromFile".locX(withComment: "empty comment")
    btnCancel.title = "btnCancel".locX(withComment: "")

I need to iterate through the file and find all pairs key-comment:

"labelPatternForExpresion" - "comment one"

"labelPathToProject" - "comment six"

........

........

"btnCancel" - ""

1
  • 1
    Most likely swift can use regular expressions like many other current programming languages. Commented Feb 26, 2017 at 20:10

1 Answer 1

1

Assuming your pattern is:

"textA" - "textB"

and you want to capture textA and textB. Use NSRegularExpression:

let str = "\"labelPatternForExpresion\" - \"comment one\""

// NSRegularExpression still deals in NSString so let's make a copy to 
// lessen the pain later
let nsStr = str as NSString
let regex = try! NSRegularExpression(pattern: "\"(.+)\" - \"(.+)\"", options: [])

if let match = regex.firstMatch(in: str, options: [], range: NSMakeRange(0, nsStr.length)) {
    let lhs = nsStr.substring(with: match.rangeAt(1))
    let rhs = nsStr.substring(with: match.rangeAt(2))
    print(lhs)
    print(rhs)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.