我第一次尝试在 C++ 中使用正则表达式,我对转义序列有点困惑。我只是想匹配字符串开头的点。为此,我使用表达式:“^\\\.”,它有效,但我的编译器(g++)生成警告:
warning: unknown escape sequence '\.'
regex self_regex("^\\\.");
^~
如果我使用例如“^\\.”,它不会生成警告,但该正则表达式与我想要做的不匹配。
我也不明白为什么我必须在“\”中使用三个反斜杠,两个反斜杠还不够吗?第一个反斜杠转义了第二个反斜杠,因此我实际上搜索 .,但它不起作用。有人可以帮我澄清一下吗?
Code:
#include <iostream>
#include <dirent.h>
#include <regex>
using namespace std;
int main(void){
DIR *dir;
string path = "/Users/-----------/Documents/Bibliothek/MachineLearning/DeepLearning/ConvolutionalNeuralNetworks/CS231n 2016/Assignments/assignment3/assignment3/cs231n";
regex self_regex("^\\\.+");
struct dirent *ent;
dir = opendir(path.c_str());
if ((dir = opendir(path.c_str())) != NULL){
while ((ent = readdir(dir)) != NULL){
if (regex_search(string(ent->d_name),self_regex)){
cout << "matches regex" << ent->d_name << endl;
}
else{
cout << "does not match regex " << ent->d_name << endl;
}
}
closedir(dir);
}
return 0;
}
Output:
matches regex.
matches regex..
matches regex.DS_Store
matches regex.gitignore
does not match regex __init__.py
does not match regex __init__.pyc
does not match regex build
does not match regex captioning_solver.py
does not match regex captioning_solver.pyc
does not match regex classifiers
does not match regex coco_utils.py
does not match regex coco_utils.pyc
does not match regex data_utils.py
does not match regex datasets
does not match regex fast_layers.py
does not match regex fast_layers.pyc
does not match regex gradient_check.py
does not match regex gradient_check.pyc
does not match regex im2col.py
does not match regex im2col.pyc
does not match regex im2col_cython.c
does not match regex im2col_cython.pyx
does not match regex im2col_cython.so
does not match regex image_utils.py
does not match regex image_utils.pyc
does not match regex layer_utils.py
does not match regex layers.py
does not match regex layers.pyc
does not match regex optim.py
does not match regex optim.pyc
does not match regex rnn_layers.py
does not match regex rnn_layers.pyc
does not match regex setup.py
当您在代码中写入字符串文字时:
"^\\\."
您的编译器将根据 C++ 规则解析它,以生成将在可执行文件中使用的字符串。例如如果\n
会遇到可执行文件中的字符串将包含换行符。这"\\"
被转化为"\"
,但是你的编译器不知道如何处理"\."
因为有没有这样的转义序列 http://en.cppreference.com/w/cpp/language/escape在 C++ 中定义。
转义序列,其中反斜杠后面的字符是
未列出的 (...) 是有条件支持的,
实现定义的语义。
因此,您要查找的字符串只有两个斜杠:
"^\\."
编译器会将其转换为:
"^\."
这就是您正在寻找的正则表达式 https://regex101.com/r/xJ4wB7/1 !
备注:例如 GCC 将转换未知的转义序列"\."
into "."
,因此 2 或 3 个反斜杠实际上会产生相同的结果。
在线演示 https://ideone.com/kbb1wq
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)