C ++ регулярное выражение, неизвестная escape-последовательность '\.' предупреждение
В первый раз я попытался использовать регулярные выражения в C ++, и меня немного смущают escape-последовательности Я просто пытаюсь сопоставить точку в начале строки. Для этого я использую выражение «^ \\\.», Которое работает, но мой компилятор (g ++) выдает предупреждение:
warning: unknown escape sequence '\.'
regex self_regex("^\\\.");
^~
Если я использую, например, «^ \\.», Это не выдает предупреждение, но это регулярное выражение не соответствует тому, что я намерен сделать.
Я также не понимаю, почему я должен использовать три обратных слеша, а двух не должно быть достаточно, в "\". первый обратный слеш избегает второго, так что я на самом деле ищу., но он не работает. Может кто-нибудь уточнить это для меня?
Код:
#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;
}
Выход:
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