Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 20 characters in body
Source Link
adazacom
  • 453
  • 3
  • 9

I came here looking for an answer to this question. Didn't find itwhat I was looking for and ended up building this by repeated calls to .split(...) It isn't elegant but you can choose which delimiters are preserved and which aren't. There's probably a way to avoid the String <--> Substring conversions, anyone know?

I came here looking for an answer to this question. Didn't find it and ended up building this by repeated calls to .split(...) It isn't elegant but you can choose which delimiters are preserved and which aren't. There's probably a way to avoid the String <--> Substring conversions, anyone know?

I came here looking for an answer to this question. Didn't find what I was looking for and ended up building this by repeated calls to .split(...) It isn't elegant but you can choose which delimiters are preserved and which aren't. There's probably a way to avoid the String <--> Substring conversions, anyone know?

added the Set solution
Source Link
adazacom
  • 453
  • 3
  • 9

In the case above, the selectors are not in actual sets. I didn't need that, but if you do, simply make these declarations,

let preservedDelimiters: Set<Character> = [ "{", "}", "(", ")", "[", "]" ] let omittedDelimiters: Set<Character> = [ " ", "\n", ",", "." ] 

and replace the whereSeparator function with:

whereSeparator: { if preservedDelimiters.contains($0) { separator = $0 return true } else if omittedDelimiters.contains($0) { separator = " " return true } else { return false } } 

In the case above, the selectors are not in actual sets. I didn't need that, but if you do, simply make these declarations,

let preservedDelimiters: Set<Character> = [ "{", "}", "(", ")", "[", "]" ] let omittedDelimiters: Set<Character> = [ " ", "\n", ",", "." ] 

and replace the whereSeparator function with:

whereSeparator: { if preservedDelimiters.contains($0) { separator = $0 return true } else if omittedDelimiters.contains($0) { separator = " " return true } else { return false } } 
added 8 characters in body
Source Link
adazacom
  • 453
  • 3
  • 9

I came here looking for an answer to this question. Didn't find it and ended up building this by repeated calls to .split(...) It isn't elegant but you can choose which delimiters are preserved and which aren't. There's probably a way to avoid the String <--> Substring conversions, anyone know?

var input = """ {All those moments will be (lost in time)}, like tears [in rain](. ([(Time to)] die)) """ var separator: Character = "!" var output: [String] = [] repeat { let tokens = input.split( maxSplits: 1, omittingEmptySubsequences: false, whereSeparator: { switch $0 { case "{", "}", "(", ")", "[", "]": // preserve separator = $0; return true case " ", "\n", ",", ".": // omit separator = " "; return true default: return false } } ) if tokens[0] != "" { output.append(String(tokens[0])) } guard tokens.count == 2 else { break } if separator != " " { output.append(String(separator)) } input = String(tokens[1]) } while true for otoken in output { print("\(otoken)") } 

I came here looking for an answer to this question. Didn't find it and ended up building this by repeated calls to .split(...) It isn't elegant but you can choose which delimiters are preserved and which aren't. There's probably a way to avoid the String <--> Substring conversions, anyone know?

var input = """ {All those moments will be (lost in time)}, like tears [in rain](. ([(Time to)] die)) """ var separator: Character = "!" var output: [String] = [] repeat { let tokens = input.split( maxSplits: 1, omittingEmptySubsequences: false, whereSeparator: { switch $0 { case "{", "}", "(", ")", "[", "]": // preserve separator = $0; return true case " ", "\n", ",", ".": // omit separator = " "; return true default: return false } } ) if tokens[0] != "" { output.append(String(tokens[0])) } guard tokens.count == 2 else { break } if separator != " " { output.append(String(separator)) } input = String(tokens[1]) } while true for o in output { print("\(o)") } 

I came here looking for an answer to this question. Didn't find it and ended up building this by repeated calls to .split(...) It isn't elegant but you can choose which delimiters are preserved and which aren't. There's probably a way to avoid the String <--> Substring conversions, anyone know?

var input = """ {All those moments will be (lost in time)}, like tears [in rain](. ([(Time to)] die)) """ var separator: Character = "!" var output: [String] = [] repeat { let tokens = input.split( maxSplits: 1, omittingEmptySubsequences: false, whereSeparator: { switch $0 { case "{", "}", "(", ")", "[", "]": // preserve separator = $0; return true case " ", "\n", ",", ".": // omit separator = " "; return true default: return false } } ) if tokens[0] != "" { output.append(String(tokens[0])) } guard tokens.count == 2 else { break } if separator != " " { output.append(String(separator)) } input = String(tokens[1]) } while true for token in output { print("\(token)") } 
added 29 characters in body
Source Link
adazacom
  • 453
  • 3
  • 9
Loading
Source Link
adazacom
  • 453
  • 3
  • 9
Loading