我想使用正则表达式来匹配 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/