How can I detect ¹ using stringr in R?

I would like to detect just ¹, ² etc in a string using stringr in R.

Later I’ll extract just it from a sentence (Like text¹ text, 2020)

library(stringr)

str_detect("¹", "[:digit:]")
#> [1] FALSE
str_detect("¹", "[:alpha:]")
#> [1] FALSE
str_detect("¹", "[:punct:]")
#> [1] FALSE

Created on 2022-11-19 with reprex v2.0.2

>Solution :

The patterns:

[⁰¹²³⁴⁵⁶⁷⁸⁹]+

or

\p{No}

will match superscript numbers (second option will also detect subscript so caution is advised, hence would recommend top)

Leave a Reply