-2

I have following strings:

"Richard H. Hoffman (60-100)" "Alex S. Simmon (72-333)" "Michael S. Pip (1-0)"

How do I use regular expression to extract only names from the string? So the results should look like:

"Richard H. Hoffman" "Alex S. Simmon" "Michael S. Pip"

Thank you

akrun
  • 674,427
  • 24
  • 381
  • 486
HardStats
  • 5
  • 1
  • 1
    [learn regex](https://stackoverflow.com/questions/4736/learning-regular-expressions) – jogo Feb 14 '18 at 08:55

2 Answers2

2

We can use sub to match zero or more spaces (\\s*) followed by ( and other characters (.*), replace with blank ("")

sub("\\s*\\(.*", "", str1)
#[1] "Richard H. Hoffman" "Alex S. Simmon"     "Michael S. Pip"

data

str1 <- c("Richard H. Hoffman (60-100)", "Alex S. Simmon (72-333)",
             "Michael S. Pip (1-0)")
akrun
  • 674,427
  • 24
  • 381
  • 486
1

You could use

gsub("^([^()]+).*", "\\1", your_vector)

This captures anything neither ( nor ) into group 1 and replaces the string with the first group, see a demo on regex101.com.

Jan
  • 38,539
  • 8
  • 41
  • 69