I want to ask a very basic question about token,
while reading about regex,the book tag caret(^) as a zero width token,
can you please tell me what actually it means by zero width?
views:
59answers:
3It means that it matches without consuming any characters. It is simply a positional assertion ("must be at the start of the line"). Another example is zero-width look-ahead and look-behind assertions. For instance, the Perl regex /abc(?=123)/
matches the sequence abc
only if it is followed by the sequence 123
, but it doesn't actually consume the 123
.
It's a zero width token because it is a token that matches the zero width string, i.e. the string containing zero characters. The number of characters in a string is sometimes called its width. It matches only the empty string if it occurs at the start of the string, or at the start of any line depending on the options.
Another example of a zero width token is \b
which matches a word boundary.
^ is just tell you a context and is not representing any physical character or characters.
^'s context is the beginning of a line.
Other examples are:
$ - end of line context
\b - word boundaries