-1

I'm trying to extract matches preceding a pattern in R. Lets say that I have a vector consisting of the next elements:

my_vector
> [1] "ABCC12|94160"        "ABCC13|150000"       "ABCC1|4363"          "ACTA1|58" 
[5] "ADNP2|22850"         "ADNP|23394"          "ARID1B|57492"        "ARID2|196528" 

I'm looking for a regular expression to extract all characters preceding the "|". The expected result must be something like this:

my_new_vector
> [1] "ABCC12"  "ABCC13"  "ABCC1"  "ACTA1"

and so on.

I have already tried using stringr functions and regular expressions based on look arounds, but I failed.

I really appreciate your advices and help to solve my issue.

Thanks in advance!

necrosnake
  • 41
  • 6

1 Answers1

1

We could use trimws and specify the whitespace as a regex that matches the | (metacharacter - so escape \\ followed by one or more character (.*)

trimws(my_vector, whitespace = "\\|.*")
akrun
  • 874,273
  • 37
  • 540
  • 662