Home > Net >  How to select everything before the first uppercase symbol that follows a lowercase symbol
How to select everything before the first uppercase symbol that follows a lowercase symbol

Time:06-07

In an example:

Hello my name is Karl, I am finHello my name is Karl, I am fine

I want to remove everything before the second Hello. So to:

Hello my name is Karl, I am fine.

It would be nice if you explain all the functions within the regex, too.

I realized that this needs to be done with str_remove().

CodePudding user response:

.*(?=[a-z][A-Z]).
  • .* Match everything till the lookahead assertion condition
  • (?= Lookahead assertion - assert that the following regex matches
    • [a-z][A-Z] Match consecutive lowercase and uppercase letters
  • ) Close lookahead
  • . Match one more character after lookahead as the last lowercase is under it

See the demo

R Example

library(stringr)

s <- "Hello my name is Karl, I am fineHello my name is Karl, I am fine"
print(str_match(s, ".*(?=[a-z][A-Z]).")[1])  # Hello my name is Karl, I am fine
  • Related