Scott Baker
Scott Baker

Reputation: 10453

.NET Regex: How do I include "end of string only" character escape in a bracket expression?

How can I put the \z character escape sequence for "end of string only" into a bracket statement like this: [:a-z]. I've tried [:a-z\z](results in a literal "z") and [:a-z\\z] (results in a literal "\" and literal "z"). I've also tried various permutations of brackets and parenthesis with similar result.

Upvotes: 1

Views: 61

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627044

Inside a character class, $ as well as \z and \Z (and \b, too) lose their "special" meaning of a zero-width assertion, and \z and \Z throw an exception as unknown escape sequences.

You can only use an alternation here:

(?:[:a-z]|\z)

This will match a : or a letter from a-z range, OR the very end of the string.

Upvotes: 1

Related Questions