Match everything to parentheses

For the string str = "Senior Software Engineer (mountain view)"

How can I combine everything until I hit the first bracket, returning me the "Senior Software Engineer"

+13
source share
6 answers

you would use ^[^\(]+ to match this and then crop it to remove the trailing space

+21
source

^[^\(]*

[^\(] is a character class that matches everything except ( , and * is a greedy match that matches the class as long as possible. ^ at the beginning matches the beginning of the line.

+7
source

To avoid trailing spaces, try ^.*?(?=\s\() .

^(.*?) indicates that it matches as few characters as possible, starting at the beginning of the line, and (?=\s\() binds the other end of the match to your guy, without capturing it or spaces in front of it.

+5
source

You can use this simple regular expression in R: *\\(.*

 str <- "Senior Software Engineer (mountain view)" sub(" *\\(.*", "", str) # [1] "Senior Software Engineer" 

It also avoids trailing spaces.

+4
source

and if you want to match everything before and after the brackets, try this:

SEARCH: ^[^\(]+|(\)).*

REPLACE FOR: \1

and if you want to match all the parantezes and delete them, try this:

SEARCH: \([^(\r\n]*?\)|\(|\)

REPLACEMENT: (LEAVE EMPTY)

0
source

Teedivers way ...

 library("stringr") 

The conclusion below will return you a list ...

 str_match_all(str, "^[^\\(]+") 

If you need a line

 str_match_all(str, "^[^\\(]+") %>% toString() 
0
source

All Articles