Szolnoki Endre Szolnoki Endre - 1 year ago 74
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 Source

I believe you want the following pattern:


See 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download