title: R: split text with multiple regex patterns and exceptions
body: <p>Would like to split a vector of character elements <code>text</code> in sentences. There are more then one pattern of splitting criteria (<code>"and/ERT"</code>, <code>"/$"</code>). Also there are exceptions(<code>:/$.</code>, <code>and/ERT then</code>, <code>./$. Smiley</code>) from the patterns.</p>
  
  <p>The try: Match the cases where the split should be. Insert an unusual pattern (<code>"^&amp;*"</code>) at that place. <code>strsplit</code> the specific pattern</p>
  
  <p>Problem: I don't know how to handle properly exceptions. There are explicit cases where the unusual pattern (<code>"^&amp;*"</code>) should be eliminated and the original text restored before running <code>strsplit</code>.</p>
  
  <p>Code:</p>
  
  <pre><code>text &lt;- c("This are faulty propositions one and/ERT two ,/$, which I want to split ./$. There are cases where I explicitly want and/ERT some where I don't want to split ./$. For example :/$. when there is an and/ERT then I don't want to split ./$. This is also one case where I dont't want to split ./$. Smiley !/$. Thank you ./$!",
  "This are the same faulty propositions one and/ERT two ,/$, which I want to split ./$. There are cases where I explicitly want and/ERT some where I don't want to split ./$. For example :/$. when there is an and/ERT then I don't want to split ./$. This is also one case where I dont't want to split ./$. Smiley !/$. Thank you ./$!",
  "Like above the same faulty propositions one and/ERT two ,/$, which I want to split ./$. There are cases where I explicitly want and/ERT some where I don't want to split ./$. For example :/$. when there is an and/ERT then I don't want to split ./$. This is also one case where I dont't want to split ./$. Smiley !/$. Thank you ./$!")
  
  patternSplit &lt;- c("and/ERT", "/\\$") # The class of split-cases is much larger then in this example. Therefore it is not possible to adress them explicitly.
  patternSplit &lt;- paste("(", paste(patternSplit, collapse = "|"), ")", sep = "")
  
  exceptionsSplit &lt;- c("\\:/\\$\\.", "and/ERT then", "\\./\\$\\. Smiley")
  exceptionsSplit &lt;- paste("(", paste(exceptionsSplit, collapse = "|"), ")", sep = "")
  
  # If you don't have exceptions, it works here. Unfortunately it splits "*$/*" into "*" and "$/*". Would be convenient to avoid this. See example "ideal" split below.
  textsplitted &lt;- strsplit(gsub(patternSplit, "^&amp;*\\1", text), "^&amp;*", fixed = TRUE) # 
  
  # Ideal split:
  textsplitted
  &gt; textsplitted
  [[1]]
   [1] "This are faulty propositions one and/ERT" 
   [2] "two ,/$," 
   [3] "which I want to split ./$."
   [4] "There are cases where I explicitly want and/ERT" 
   [5] "some where I don't want to split ./$." 
   [6] "For example :/$. when there is an and/ERT then I don't want to split ./$."
   [7] "This is also one case where I dont't want to split ./$. Smiley !/$." 
   [8] "Thank you ./$!"
  
  [[2]]
   [1] "This are the same faulty propositions one and/ERT 
   [2] "two ,/$,"
  #...      
  
  # This try doesen't work!
  text &lt;- gsub(patternSplit, "^&amp;*\\1", text)
  text &lt;- gsub(exceptionsSplit, "[original text without "^&amp;*"]", text)
  textsplitted &lt;- strsplit(text, "^&amp;*", fixed = TRUE)
  </code></pre>