In regex, \d
means a digit, and +
means "one or more". So the pattern "\d+"
matches one or more digits. We can use stringr::str_extract
with this pattern to extract a number - by default the pattern match will be extracted (as desired). Using regex in R, we need to escape the \
in the pattern with a second \
:
str_extract("Some text 7/8\n", "\\d+")
#[1] "7"
In the case where the preceding text may include numbers, I'd recommend two stage process - first extract the number followed by a /
(just add it to the end of the regex pattern), then replace the extracted /
with a blank.
result = str_extract("Some 2879 numbery 8972 text 7/8\n", "\\d+/")
result = str_replace(result, pattern = "/", replacemet = "")
result
#[1] "7"
If you want to worry about the case where the preceding text might have a fraction in it, we'll need to think harder about how to pull out the correct numerator. If it's always the last fraction that needs to be extracted, we could use stringi::stri_extract_last_regex
instead of stringr::str_extract
. If it isn't consistently the last one, then you'll need to work out some logic to figure out which one to use...