I was asked by a colleague why ⎕S
reports two matches in the following example:
('\d*'⎕S 0 1)'321'
┌───┬───┐
│0 3│3 0│
└───┴───┘
Here we are asking for the position and length of sequences of zero or more digits in an input document containing three numeric characters. Intuitively there is just one match of all three characters from the start of the sequence, but ⎕S
reports an additional match of zero characters at the end.
The short explanation is this: when there is a match in the input, the matching characters are “consumed” and searching continues from after them. In this example the first match consumes all three characters in the input, leaving it empty. Searching then resumes and, as the pattern matches the zero remaining characters, there is a second match.
If it seems odd that we search when the input is empty then consider the case where the input is empty to start with:
('\d*'⎕S 0 1)''
┌───┐
│0 0│
└───┘
In this case we should not be surprised there is a match – the pattern explicitly allows it and, indeed, we would have coded the search pattern differently, perhaps as '\d+'
, if that was not wanted. It is therefore correct and consistent that the first example matches zero characters as well.
My colleague was not yet convinced. In this case, he asked, why are there not an infinite number of matches at the end?
It’s a good question. Zero-length matches have to be treated specially by the search engine to prevent exactly that, wherever they appear. The rule used by the PCRE search engine, which Dyalog uses, is that any pattern that results in a zero-length match is prohibited from generating another zero-length match until more characters are consumed from the input. PCRE is widely used so this behaviour is commonplace, but other search engine implementations take a different view and simply consume a single character following a zero-length match to prevent the repetition.
That difference in the rules would not have affected our example but consider a slightly more complex search pattern containing alternatives – here, zero or more digits, or one word character:
('\d*|\w'⎕S 0 1)'x321'
┌───┬───┬───┬───┐
│0 0│0 1│1 3│4 0│
└───┴───┴───┴───┘
In this example the first match is of zero digits at the start of the input – that is, at offset zero and zero characters long. PCRE then resumes searching at offset zero but without allowing further zero-length matches. Consequently the first alternative pattern cannot match this time but the second can. The next match is therefore a single word character (the 'x'
), also at the start of the input. The 'x'
is consumed and searching resumes with '321'
as the input where, as explained above, there are two further matches making four in total.
Search engines which simply consume a character after a zero-length match would give a different result. After the same initial match of zero characters at the start of the input they would consume the 'x'
and resume searching at '321'
to give two further matches and a total of only three.
So, patterns which allow zero-length matches appear to be counter-intuitive and the cause of incompatibilities with other search engines. Should we avoid '*'
and other qualifiers which allow zero repetitions of a pattern? Of course not – used carefully they can be very effective.
Firstly, a pattern which allows a sequence of zero characters within it can still be constructed so that the overall match can never have a length of zero. For example, 'colou?r'
allows zero or one 'u'
characters after the second 'o'
so that it matches both the British English spelling 'colour'
and the American English spelling 'color'
– but it never results in a zero-length match, and should never surprise anyone in the way it works.
Secondly, anchors can often be used to prevent those unintuitive extra matches. Anchors in search patterns don’t match actual characters – rather, they allow the match to succeed only at specific locations in the input. '^'
and '$'
mean the start and end of a line respectively which, used in our very first example, prevent anything from preceding or following a match within a line and exclude the zero-length sequence because it does not satisfy that requirement:
('^\d*$'⎕S 0 1)'321'
┌───┐
│0 3│
└───┘
Thirdly – it often doesn’t matter that zero-length matches occur. In this example the search pattern matches any character except newline zero or more times and we are asking for it to be folded to upper-case:
('.*' ⎕R '\u&') 'Hello there'
HELLO THERE
The first match is of all 11 characters which are folded to upper-case and replaced. Then is there a second match of zero characters at the end and this too is folded and replaced, having no further effect. You can see this is so by using a different replacement pattern:
('.*' ⎕R '[\u&]') 'Hello there'
[HELLO THERE][]
As an aside, a ⎕R
search pattern constructed so that it has no visible effect at all can be very useful for doing a quick and simple read of a text file into the workspace. In this example:
tieno←⎕NTIE 'input.txt'
''⎕R''⊢tieno
text is read from the file without modification into a vector of character vectors, one line per element. There are many search and replace patterns that could be used to do this but this is the shortest, and it makes use of zero-length matches and replacements.
Perhaps, after all, the title of this blog should have been “zero-length regex matches considered helpful“?
Further Reading
Further information about the Dyalog search and replace operators is available in the following documents:
Footnote
Edsger W. Dijkstra wrote a paper in 1968 for Communications of the ACM entitled a “Case against the GO TO Statement” but it was published as a letter under the title “The goto statement considered harmful”. “Considered harmful” became a popular phrase in articles written in response to this and in other unrelated papers. There is an article about the phrase in Wikipedia.
Dijkstra also wrote a 1975 paper in which he criticised a number of computer languages he had used. Of APL he said, “APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums”.
This author is not afraid to use goto statements or APL.