xingbin
xingbin

Reputation: 28279

Does zero-length matches end the match procedure?

If I have a regex a? and a string a, I will get:

Enter your regex: a?
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.

The match procedure ends after a zero-length match. Otherwise I will get infinite:

I found the text "" starting at index 1 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
.....

My question is, does Zero-Length Matches always end the match procedure? Is there any other situation?

Upvotes: 3

Views: 100

Answers (1)

revo
revo

Reputation: 48711

My question is, does Zero-Length Matches always end the match procedure?

No, your input string is made up of one single character a so it matches one zero-length position right after it, more characters lead into more matches.

The match procedure ends after a zero-length match. Otherwise I will get infinite.

It's up to RegEx engine. Different flavors handle zero-length matches in different ways. They don't let infinite matches at same position happen though:

Perl , PCRE is to always start the next match attempt at the end of the previous match, regardless of whether it was zero-length or not...

Python advances after zero-length matches. The gsub() function to search-and-replace skips zero-length matches at the position where the previous non-zero-length match ended.

More on zero-length matches

Upvotes: 1

Related Questions