You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含负向前瞻与^的C++正则表达式在macOS匹配但Linux不匹配的原因?

为何带负向前瞻的同一正则表达式在macOS/Windows匹配,Linux下不匹配?

测试用C++示例代码

#include <iostream>
#include <regex>

int main() {
  std::regex rx("^(((?!(\\/|^)\\.).)*?)$");
  std::string input = "foo.bar";

  std::cout << std::regex_match(input, rx) << std::endl;
}

各平台运行结果

  • macOS与Windows:1
  • Linux(GCC 11测试):0

问题排查细节

问题根源在于负向前瞻(?!(\/|^)),移除其中的|^后,各平台匹配结果一致。尝试使用std::regex_match(input, rx, std::regex_constants::match_not_bol)参数并未解决问题。

有意思的是,当目标字符串不含.时,各平台匹配结果再次统一;且同一正则表达式在JavaScript中各平台均能正常匹配。

使用的编译器版本详情

  • macOS: Apple clang version 14.0.0 (clang-1400.0.29.202)
  • Linux: g++-11 (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
  • Windows: Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x86

内容的提问来源于stack exchange,提问作者Benjamin Pasero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 01:01:21