regex - Regular Expression not returning expected result in Swift -
i hav following, gets matches of string in string:
let : string = "https://www.example.com" let b = nsstring let regex : nsregularexpression = try! nsregularexpression(pattern: "(\\.(.*)\\. || /(.*)\\.)", options: .caseinsensitive) let text = regex.matchesinstring(a, options: [], range: nsmakerange(0, b.length)) return text.map {b.substringwithrange($0.range)}[0].stringbyreplacingoccurrencesofstring(".", withstring: "")
the goal create more readable version of domain name. instance - https://www.example.com should become: example
but returns empty string.
is there wrong regular expression?
you have spaces in regex pattern.
you using ||. not mean or in regex pattern.
you nesting parens.
i modified regex , it's doing expect.
let : string = "https://www.example.com" let b = nsstring let regex : nsregularexpression = try! nsregularexpression(pattern: "\\.(.*)\\.", options: .caseinsensitive) let text = regex.matchesinstring(a, options: [], range: nsmakerange(0, b.length)) return text.map {b.substringwithrange($0.range)}[0].stringbyreplacingoccurrencesofstring(".", withstring: "")
outputs:
"example"
Comments
Post a Comment