Home > Blockchain >  How can I detect ¹ using stringr in R?
How can I detect ¹ using stringr in R?

Time:11-19

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

CodePudding user response:

How about this negative character class matching anything that is not ASCII?

str_detect(c("%", "f", "1", "¹"), "[^ -~]")
[1] FALSE FALSE FALSE  TRUE

CodePudding user response:

The patterns:

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

or

\p{No}

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

  • Related