c++ - 使用正则表达式排除 C++ 中的子文件夹

标签 c++ regex macos

我想使用正则表达式来匹配 macOS 上的这个文件路径:

/Users/username/Library/Containers/com.tencent.xinWeChat/...

这个路径下有很多级的子文件夹,但是我想排除这个特定的路径。

/Users/username/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/8be628ba3eab5f8c52f6f1b22f897153/Message/MessageTemp/8be628ba3eab5f8c52f6f1b22f897153/OpenData/...

我想在正则表达式中排除包含“OpenData”的路径,所以我使用这个:

R"^/Users/[^/]+/Library/Containers/com\.tencent\.\w+/.+/(?!OpenData)/.+"

但它不起作用。 消极的前瞻对我来说有点难。 谁能告诉我正确的正则表达式?谢谢!

最佳答案

你可以使用

^/Users/[^/]+/Library/Containers/com\.tencent\.\w+/(?!(?:.*/)?OpenData(?:/|$)).+

参见 regex demo

详情

  • ^ - 字符串的开始
  • /Users/ - 文字子串
  • [^/]+ - 除了 /
  • 之外的 1+ 个字符
  • /Library/Containers/com\.tencent\. - /Library/Containers/com.tencent. 字符串
  • \w+ - 1+ 个单词字符
  • / - / 字符
  • (?!(?:.*/)?OpenData(?:/|$)) - 如果存在 OpenData,则匹配失败的否定先行URL 中的子部分
  • .+ - 除换行字符外的任何 1+ 个字符,尽可能多。

关于c++ - 使用正则表达式排除 C++ 中的子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430987/

相关文章:

c++ - 在填充到结构中之前解码提取的表达式

c++ - 测试虚函数的开销

php - 在 codeigniter 中使用正则表达式验证表单

php - 正则表达式的工作方式不同

java - 在 MacOS 上安装 clojure

python - 值错误 : 'vertices' must be a 2D list or array with shape Nx2

c# - 使用tcp将 float 组从c++发送到c#

c++ - 如何使用 boost::asio 即时更改串行端口速度(或如何确定硬件缓冲区是否为空)?

Python 正则表达式 - 后视断言必须固定宽度

macos - mac系统级git配置