用Regex讀取INI檔案的執行個體教程

來源:互聯網
上載者:User
看到了一堆調用Windows API的方式來讀寫INI檔案,來嘗試一次用Regex的方法。沒有太多的內容,定義的幾個粗糙的Regex,來讀取INI中的內容。現在只是為了嘗試一下,做的比較爛,廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:

#include "stdio.h" #include <sstream> #include <iostream> #include <fstream> #include <regex> using namespace std; void Trim(char * str); void lTrim(char * str); void rTrim(char * str); // 測試sscanf 和 Regex // sscanf提供的這個擴充功能其實並不能真正稱為Regex,因為他的書寫還是離不開% // []表示字元範圍,{}表示重複次數,^表示取非,*表示跳過。所以上面這個url的解析可以寫成下面這個樣子: // //char url[] = "dv://192.168.1.253:65001/1/1" // //sscanf(url, "%[^://]%*c%*c%*c%[^:]%*c%d%*c%d%*c%d", protocol, ip, port, chn, type); // //解釋一下 //先取得一個最長的字串,但不包括字串 ://,於是protocol="dv\0"; //然後跳過三個字元,(%*c)其實就是跳過 :// // 接著取一個字串不包括字串 : ,於是ip = 192.168.1.253,這裡簡化處理了,IP就當個字串來弄,而且不做檢查 // 然後跳過冒號取連接埠到port,再跳過 / 取通道號到chn,再跳過 / 取碼流類型到type。 // c語言實現上例 void test1() {  char url[] = "dv://192.168.1.253:65001/1/1";  char protocol[10];  char ip[17];  int port;  int chn;  int type;  sscanf(url, "%[^://]%*c%*c%*c%[^:]%*c%d%*c%d%*c%d", protocol, ip, &port, &chn, &type);  printf("%s, %s, %d, %d, %d\n", protocol, ip, port, chn, type); } // 讀取ini裡某行字串, 得到: hello world! // 正常串1: -claim="hello world!" // 正常串2: claim = "hello world!" // 正常串3: claim = " hello world!" // 正常串4: claim_ = hello world! // 幹擾串1: cl-aim = \"hello world!" // 幹擾串2: clai3m = "hello world!\" // 幹擾串3: cla_im = \\"hello world!\" // 幹擾串4: claim ='"hello world!\" // 幹擾串5: claim= @"\nhello world!" // 幹擾串6: claim=L"hello world!" // 未處理1: claim[1] = 1 // 未處理1: claim[2] = 1 void test2() {  char line[1000] = { 0 };  char val[1000] = { 0 };  char key[1000] = { 0 };  FILE *fp = fopen("1.txt", "r");  if (NULL == fp)  {   printf("failed to open 1.txt\n");   return ;  }  while (!feof(fp))  {   memset(line, 0, sizeof(line));   fgets(line, sizeof(line) - 1, fp); // 包含了每行的\n   printf("%s", line);   Trim(line);   // 提取等號之前的內容   memset(key, 0, sizeof(key));   // sscanf使用的format不是Regex,不能用 \\s 表示各種空白符,即空格或\t,\n,\r,\f   sscanf(line, "%[^ \t\n\r\f=]", key);   //sscanf(line, "%*[^a-zA-Z0-9_-]%[^ \t\n\r\f=]", key);   printf(" key: [%s]\n", key);   // 提取等號之後的內容   memset(val, 0, sizeof(val));   sscanf(line, "%*[^=]%*c%[^\n]", val); // 不包含了每行的分行符號   Trim(val);   printf(" val: [%s]\n", val);   // 去除兩邊雙引號   // ...   // 插入map   // map[key]=value;   // string 轉 其它類型   // atoi, atol, atof  }  printf("\n");  fclose(fp); } // 上例的C++實現 template<class T1, class T2> inline T1 parseTo(const T2 t) {  static stringstream sstream;  T1 r;  sstream << t;  sstream >> r;  sstream.clear();  return r; } void test3() {  char val[1000] = { 0 };  char key[1000] = { 0 };  ifstream fin("1.txt");  string line;  if (fin)  {   while (getline(fin, line)) // line中不包括每行的分行符號   {    cout << line << endl;    /// 提取等號之前的內容    // 第1組()表示任意個空白字元,第2組()表示單詞(可帶_或-),    // 第3組()表示1個以上的空白字元(或=),最後以任一字元串結尾    regex reg("^([\\s]*)([\\w\\-\\_]+)([\\s=]+).*$");    // 取第2組代替原串    string key = regex_replace(line, reg, "$2");    cout << " key: {" << key << "}" << endl;    /// 提取等號之後的內容    // 第1組()表示任意個空白字元,第2組()表示單詞(可帶_或-),    // 第3組()表示1個以上的空白字元(或=),第4組()表示任意個字元,    // 第5組()表示以任意個空白字元(或斷行符號分行符號)結尾。    reg = regex("^([\\s]*)([\\w\\-\\_]+)([\\s=]+)(.*)([\\s\\r\\n]*)$");    // 取第4組代替原串    string val = regex_replace(line, reg, "$4");    cout << " val: {" << val << "}" << endl;    // 去除兩邊雙引號    // ...    // 插入map    // map[key]=value;    // string 轉 其它類型    // int i = parseTo<int>("123");    // float f = parseTo<float>("1.23");    // string str = parseTo<string>(123);   }  }  else // 沒有該檔案  {   cout << "no such file" << endl;  } } void main() {  //test1();  test2();  test3(); } void lTrim(char * str) {  int i, len;  len = strlen(str);  for (i = 0; i<len; i++)  {   if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && str[i] != '\r' && str[i] != '\f') break;  }  memmove(str, str + i, len - i + 1);  return; } void rTrim(char * str) {  int i, len;  len = strlen(str);  for (i = len - 1; i >= 0; i--)  {   if ((str[i] != ' ') && (str[i] != 0x0a) && (str[i] != 0x0d) && (str[i] != '\t') && (str[i] != '\f')) break;  }  str[i + 1] = 0;  return; } void Trim(char * str) {  int i, len;  //先去除左邊的空格  len = strlen(str);  for (i = 0; i<len; i++)  {   if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && str[i] != '\r' && str[i] != '\f') break;  }  memmove(str, str + i, len - i + 1);  //再去除右邊的空格  len = strlen(str);  for (i = len - 1; i >= 0; i--)  {   if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && str[i] != '\r' && str[i] != '\f') break;  }  str[i + 1] = 0;  return; } /* void Trim(char * str) {  lTrim(str);  rTrim(str); } */


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.