title: Truncating the end of a string in R after a character that can be present zero or more times
body: <p>I have the following data:</p>
  
  <pre><code>temp&lt;-c("AIR BAGS:FRONTAL" ,"SERVICE BRAKES HYDRAULIC:ANTILOCK",
      "PARKING BRAKE:CONVENTIONAL",
      "SEATS:FRONT ASSEMBLY:POWER ADJUST",
      "POWER TRAIN:AUTOMATIC TRANSMISSION",
      "SUSPENSION",
      "ENGINE AND ENGINE COOLING:ENGINE",
      "SERVICE BRAKES HYDRAULIC:ANTILOCK",
      "SUSPENSION:FRONT",
      "ENGINE AND ENGINE COOLING:ENGINE",
      "VISIBILITY:WINDSHIELD WIPER/WASHER:LINKAGES")
  </code></pre>
  
  <p>I would like to create a new vector that retains only the text before the first ":" in the cases where a ":" is present, and the whole word when ":" is not present.</p>
  
  <p>I have tried to use: </p>
  
  <pre><code>temp=data.frame(matrix(unlist(str_split(temp,pattern=":",n=2)), 
  +                        ncol=2, byrow=TRUE))
  </code></pre>
  
  <p>but it does not work in the cases where there is no ":"</p>
  
  <p>I know this question is very similar to:
  <a href="https://stackoverflow.com/questions/6861740/truncate-string-from-a-certain-character-in-r">truncate string from a certain character in R</a>, which used:</p>
  
  <pre><code>sub("^[^.]*", "", x)
  </code></pre>
  
  <p>But I am not very familiar with regular expressions and have struggled to reverse that example to retain only the beginning of the string.</p>