On the use of OR | operator in regular expression

I had some difficulties to use the OR | operator in regular expressions.
Let do some exercises and examine the results:
gregexpr("bc", "abcd") : return 2; normal, bc is located at the second position
gregexpr("b|c", "abcd") : return 2 3; | is the OR operator and it will return the position of b OR c
this is the same than :
gregexpr("[bc]", "abcd") or gregexpr("[b|c]", "abcd")

But | and [] are not always the same:

Let do more complicated:
gregexpr("ab|cd", "abcd")
My problem was about the priority order of | operator. The return here is 1 3
It means that the search was for a followed by b OR c followed by d.

Let do more complicated :
gregexpr("a[bc]d", "abcd") : return -1; it search for abd or acd. None exist

In practice, it is good to remember that | separates blocks of comparisons.

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04