Skip to content

Glob class [^a] is read as "a or ^", not as negation: x[^a] matches "xa" and "x^" but not "xb" #158

Closed

Description

@matt-edmondson

What's wrong

In bash, zsh, gitignore and minimatch, a character class that opens with ^ is negated, the same as [!...]. Regex uses ^ for negation too, and the filter box offers both glob and regex. DotNet.Glob doesn't treat ^ as negation, and TextFilter doesn't rewrite it before parsing. As a result [^a] turns into a positive class with two members, ^ and a. The user gets back exactly the items they meant to exclude, and nothing tells them so.

^ already means negation elsewhere in this filter syntax: a leading ^ marks an excluded token (ExcludedTokenPrefixes, TextFilter/TextFilter.cs:83). That makes it all the more surprising that ^ doesn't negate inside a class.

Reproduction

Tested on main @ 3b3784e with .NET 10, using TextFilter.IsMatch(text, filter) with the defaults (Glob, ByWordAny, CaseSensitive):

Filter Text Result Expected
x[^a] xb False True
x[^a] xa True False
x[^a] x^ True False
x[!a] xb True True

Suggested fix

MoveTrailingClassDashes (TextFilter/TextFilter.cs:474-518) already walks every closed class. It also recognises a leading ! (lines 492-493). Give a leading ^ the same treatment and emit ! in its place, so that [^a] becomes [!a] and [^a-] becomes [!-a]. Leave an unclosed class untouched, as happens today.

Open PR #156 reworks class handling and doesn't touch this. This fix should either land after that PR or be folded into it.

Acceptance criteria

  • IsMatch("xb", "x[^a]") returns true.
  • IsMatch("xa", "x[^a]") and IsMatch("x^", "x[^a]") return false.
  • [^a-] behaves like [!a-].
  • A ^ in any position other than first in a class is still a literal: x[a^] matches x^.
  • The behaviour is the same under CaseInsensitive.
  • The Glob hint or the README mentions both [!...] and [^...].

Activity

  1. added
    bug
    on Oct 29, 2026
  2. matt-edmondson commented on Oct 29, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  3. added 2 commits that reference this issue on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions