I need to extract the first part of a text, which is uppercase till the first letter lowercase.
For example, I have the text: "IV LONG TEXT HERE and now the Text End HERE"
I want to extract the "IV LONG TEXT HERE".
I have been trying something like this:
text <- "IV LONG TEXT HERE and now the Text End HERE"
stringr::str_extract_all(text, "[A-Z]")
but I’m failing at the regex.
>Solution :
You could use str_extract, with a pattern to match a single uppercase char and optionally match spaces and uppercase chars ending with another uppercase char.
\b[A-Z](?:[A-Z ]*[A-Z])?\b
Explanation
\b[A-Z]A word boundary to prevent a partial word match, then match a single char A-Z(?:Non capture group to match as a whole[A-Z ]*[A-Z]Match optional chars A-Z or a space and match a char A-Z
)?Close the non capture group and make it optional\bA word boundary
Example
text <- "IV LONG TEXT HERE and now the Text End HERE"
stringr::str_extract(text, "\\b[A-Z](?:[A-Z ]*[A-Z])?\\b")
Output
[1] "IV LONG TEXT HERE"