-2

Why does my regex stops working when the end anchor ($) is preceeded by an optional caracter anchor (?)? For example

library(tidyverse
s   <- '[S|s][EG|eg]?'
s2  <- '^[S|s][EG|eg]?$'

[1] TRUE TRUE
c('s','Seg') %>% str_detect(s2) 
[1]  TRUE FALSE

Wrapping the regex with [ ] also did not work

s3  <- '^[[S|s][EG|eg]?]$'
c('s','Seg') %>% str_detect(s) 
c('s','Seg') %>% str_detect(s3) 
[1]  TRUE FALSE
LucasMation
  • 2,408
  • 2
  • 22
  • 45

1 Answers1

3

^[[S|s][EG|eg]?]$ matches

  • a single character in the list:

[ S | s

  • and one optionnal character in the list:

E G | e g

  • and a closing square bracket ]

https://regex101.com/r/Pr4IUq/1

Toto
  • 89,455
  • 62
  • 89
  • 125