Szolnoki Endre Szolnoki Endre - 1 month ago 9
R Question

Unexpected regular expression result in stringr (R)

Would you somebody please explain it to me, why does str_detect (from the stringr package, ver 1.1.0) return TRUE for each of the three following codes, contrary to my expectations?

str_detect("01", "^[0]*[1-9]*[0]+")
str_detect("01", "^0*[1-9]*0+")
str_detect("01", "^0*[1-9]*0")


I wanted to look for any zeroes at the beginning followed by at least 1 non-zero number and later a zero in the string.

Clearly the "01" string cannot qualify as it does not have a 0 after the 1.

Am I missing something? Is the pattern wrong for what I am looking for?

Thank you for your time!

Answer

I believe you want the following pattern:

^0[1-9]+0

See https://regex101.com/r/v9cwHJ/1 for full pattern explanation.
Your specific error was using * for the first 0, it matches none as well.
Also use + for the second digit to find at least 1.

Comments