29

I would like to remove everything after a space in a string.

For example:

"my string is sad"

should return

"my"

I've been trying to figure out how to do this using sub/gsub but have been unsuccessful so far.

zx8754
  • 52,746
  • 12
  • 114
  • 209
user1214864
  • 293
  • 1
  • 3
  • 4

5 Answers5

35

You may use a regex like

sub(" .*", "", x)

See the regex demo.

Here, sub will only perform a single search and replace operation, the .* pattern will find the first space (since the regex engine is searching strings from left to right) and .* matches any zero or more characters (in TRE regex flavor, even including line break chars, beware when using perl=TRUE, then it is not the case) as many as possible, up to the string end.

Some variations:

sub("[[:space:]].*", "", x) # \s or [[:space:]] will match more whitespace chars
sub("(*UCP)(?s)\\s.*", "", x, perl=TRUE) # PCRE Unicode-aware regex
stringr::str_replace(x, "(?s) .*", "")   # (?s) will force . to match any chars

See the online R demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • For what it's worth I recommend trying the `sub("(*UCP)(?s)\\s.*", "", x, perl=TRUE)` variation. Pleased to find this works where other variations might fail. – Pake Jul 29 '22 at 16:11
14
strsplit("my string is sad"," ")[[1]][1]
baptiste
  • 75,767
  • 19
  • 198
  • 294
12

or, substitute everything behind the first space to nothing:

gsub(' [A-z ]*', '' , 'my string is sad')

And with numbers:

gsub('([0-9]+) .*', '\\1', c('c123123123 0320.1'))
M. Beausoleil
  • 3,141
  • 6
  • 29
  • 61
SeeLittle
  • 606
  • 3
  • 7
4

Stringr is your friend.

library(stringr)
word("my string is sad", 1)
vladiim
  • 1,862
  • 2
  • 20
  • 27
3

If you want to do it with a regex:

gsub('([A-z]+) .*', '\\1', 'my string is sad')
Justin
  • 42,475
  • 9
  • 93
  • 111