Home > Mobile >  Regex match after last / and first underscore
Regex match after last / and first underscore

Time:03-08

Assuming I have the following string:

string = "path/stack/over_flow/Pedro_account"

I am intrested in matching the first 2 characters after the last / and before the first _. So in this case the desired out put is:

Pe

What I have so far is a mix of substr and str_extract:

substr(str_extract(string, "[^/]*$"),1,2)

which of course will give an answer but I belive there is a nice regex for it as well, and that is what I'm looking for.

CodePudding user response:

You can use

library(stringr)
str_extract(string, "(?<=/)[^/]{2}(?=[^/]*$)")
## => [1] "Pe"

See the R demo and the regex demo. Details:

  • (?<=/) - a location immediately preceded with a / char
  • [^/]{2} - two chars other than /
  • (?=[^/]*$) - a location immediately preceded with zero or more chars other than / till the end of string.

CodePudding user response:

Using basename to get the last folder name, then substring:

substr(basename("path/stack/over_flow/Pedro_account"), 1, 2)
# [1] "Pe"

CodePudding user response:

Remove everything till last / and extract first 2 characters.

Base R -

string = "path/stack/over_flow/Pedro_account"
substr(sub('.*/', '', string), 1, 2)
#[1] "Pe"

stringr

substr(stringr::str_remove(string, '.*/'), 1, 2)

CodePudding user response:

You can use str_match with a capture group:

  • / Match literally
  • ([^/_]{2}) Capture 2 chars other than / or _ in group 1
  • [^/]* Match optional chars other than /
  • $ End of string

See a regex demo and a R demo.

Example

library(stringr)
string = "path/stack/over_flow/Pedro_account"
str_match(string, "/([^/_]{2})[^/]*$")[,2]

Output

[1] "Pe"
  • Related