BMH
BMH

Reputation: 11

Why does the Java regular expression "|" find a matching substring for any input string?

I am trying to understand why a regular expression ending with "|" (or simply "|" itself) will find a matching substring with start index 0 and end "offset after the last character matched (as per JavaDoc for Matcher)" 0.

The following code demonstrates this:

public static void main(String[] args) {

    String regExp = "|";
    String toMatch = "A";
    Matcher m = Pattern.compile(regExp).matcher(toMatch);
    System.out.println("ReqExp: " + regExp + 
            " found " + toMatch + "(" + m.find() + ") " +  
            " start: " + m.start() +
            " end: " + m.end());

}

Output is:

ReqExp: | found A(true)  start: 0 end: 0

I'm confused by the fact that it is even a valid regular expression. And further confused by the fact that start and end are both 0.

Hoping someone can explain this to me.

Upvotes: 0

Views: 48

Answers (1)

user94559
user94559

Reputation: 60143

The pipe in a regular expression means "or." So your regular expression is basically "(empty string) or (empty string)". It successfully finds an empty string at the beginning of the string, and an empty string has a length of 0.

Upvotes: 4

Related Questions