Skip to content

grep/sed: [^:] and [^x] stop after the first byte of a multi-byte character #1907

Backlog

Description

@ShiomiHC
export LC_ALL=C.UTF-8
printf '可开=T\n' | grep -o '可[^:]*'
printf '可开=T\n' | grep -c '可[^:]*='
printf '开\n'     | grep -c '^[^x]$'
printf 'a:b\n'   | grep -o '[:]'
printf '开:x\n'  | sed 's/[^:]*/X/'
printf 'a中b\n'   | sed 's/[一-龥]/X/g'

WinuxCmd:

grep: (standard input): binary file matches
0
0
grep: (standard input): binary file matches
X��:x
aXXXb

GNU grep 3.0 / GNU sed 4.9 (Git for Windows):

可开=T
1
1
:
X:x
aXb

: is ef bc 9a and 开 is e5 bc 80. With -a, the first command prints e5 8f af e5 (可 plus the first byte of 开), and printf '开\n' | grep -c '^[^x][^x][^x]$' prints 1. grep -P gives the GNU output, and LC_ALL makes no difference. . is not affected: printf '中\n' | grep -c '^.$' prints 1.

The grep -c lines are the ones that hurt: the line is dropped without any message.

WinuxCmd 1.2.3 (WinuxCmd-1.2.3-win-x64.tar.gz) and 1.2.1, Windows 11.

The issue was drafted with Claude and the reproductions were run on the versions above.

Activity

  1. caomengxuan666 commented on Nov 28, 2026

    @caomengxuan666
    Member

    队长分拣:确认本方 bug(存量问题,非新回归;建议 P2)。

    队长在 X299 装机 WinuxCmd 1.2.1 上实跑全部复现(与报告人的 1.2.3 结论一致,1.2.1 同坏);GNU 侧为云端 oracle(grep 3.12 / sed 4.9,LC_ALL=C.UTF-8;CJK 区间例另以 en_US.UTF-8 复核,GNU 得 aXb):

    形 GNU WinuxCmd 1.2.1(队长实测)
    grep -ao '可[^:]*'(od) e5 8f af e5 bc 80 3d 54(可开=T) e5 8f af e5(可 + 开的首字节)
    grep -c '可[^:]*=' 1 0(rc=1,整行静默丢弃)
    grep -c '^[^x]$'(输入 开) 1 0
    grep -c '^[^x][^x][^x]$'(输入 开) 0 1(按字节数凑满)
    grep -ao '[:]'(od) 一个字符匹配 ef 0a bc 0a 9a 0a(三个单字节匹配)
    sed 's/[^:]*/X/'(开:x) X:x X + 坏字节 + :x
    sed 's/[一-龥]/X/g'(a中b) aXb aXXXb(中 的三个字节各替换一次)

    . 不受影响(^.$ 两侧均 1),与报告一致。

    定性:winux-regex 的括号类是字节集合——否定类在多字节字符的首个被排除字节处截断、肯定类逐字节命中。修复方向:类匹配改为按字符(UTF-8 解码)求值,至少覆盖否定类与多字节端点区间。与 #1910(量词只绑末字节)、#1909(-i 仅 ASCII 折叠)同属 winux-regex 字节化根因,建议同波处理。

    — 队长分拣

  2. added
    bug
    compat-verified
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompat-verified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions