C++11--正則表達式


/* 
正則表達式:一個指定的模式用來對文本中的字符串提供精確且靈活的匹配
*/

#include <regex>
using namespace std;

int main() {
   string str;
   while (true) {
      cin >> str;
	  //regex e("abc.", regex_constants::icase);   // .   表示除了換行符之外任意字符
	  //regex e("abc?");               // ?       0個或者1個前面的字符
	  //regex e("abc*");               // *       0個或多個前面的字符
	  //regex e("abc+");               // +       1個或多個前面的字符
	  //regex e("ab[cd]*");            // [...]   方括號中任意字符
	  //regex e("ab[^cd]*");           // [^...]   任意不在方括號中的字符
	  //regex e("ab[cd]{3,5}");
	  //regex e("abc|de[\]fg]");         // |       或者
	  //regex  e("(abc)de+\\1");       // \1      第1個子串
	  //regex  e("(ab)c(de+)\\2\\1");
	  //regex e("[[:w:]]+@[[:w:]]+\.com"); // [[:w:]] :字母,數字,下划線

	  //regex e("abc.$");                 // $   行尾
	  regex e("^abc.+", regex_constants::grep);                 // ^   行首,切換正則表達式語法
	  

	  //bool match = regex_match(str, e);    //str和e精確匹配
	  bool match = regex_search(str, e);        //str中中包含e

	  cout << (match? "Matched" : "Not matched") << endl << endl;
   }
}

/*

正則表達式語法:

   ECMAScript    //C++默認
   basic
   extended
   awk
   grep 
   egrep

regex e("^abc.+", regex_constants::grep);     //    切換語法
 */

/***************  處理子表達式 *****************/

/* 
  std::match_results<>  儲存詳細的匹配Store the detailed matches
  smatch                string類型的詳細的匹配Detailed match in string

  smatch m;
  m[0].str()   整個匹配的字符串 (同m.str(), m.str(0))
  m[1].str()   第1個子串(同m.str(1))
  m[2].str()   第2個子串
  m.prefix()   所有匹配字符串之前的部分
  m.suffix()   所有匹配字符串之后的部分
*/

int main() {
   string str;

   while (true) {
      cin >> str;
	  smatch m;        // typedef std::match_results<string>

	  regex e("([[:w:]]+)@([[:w:]]+)\.com");  

	  bool found = regex_search(str, m, e);        //只返回第一個匹配

      cout << "m.size() " << m.size() << endl;        //size()==子匹配個數+1
	  for (int n = 0; n< m.size(); n++) {
		   cout << "m[" << n << "]: str()=" << m[n].str() << endl;
		   cout << "m[" << n << "]: str()=" << m.str(n) << endl;
			cout << "m[" << n << "]: str()=" << *(m.begin()+n) << endl;
	  }
	  cout << "m.prefix().str(): " << m.prefix().str() << endl;
	  cout << "m.suffix().str(): " << m.suffix().str() << endl;
   }
}



// 多個匹配的情況

/**************** Regex Iterator ******************/
int main() {
	cout << "Hi" << endl;

   string str;

   while (true) {
      cin >> str;

	  regex e("([[:w:]]+)@([[:w:]]+)\.com"); 
	  
	  sregex_iterator pos(str.cbegin(), str.cend(), e);
	  sregex_iterator end;  // 默認構造定義了past-the-end迭代器
	  for (; pos!=end; pos++) {
		  cout << "Matched:  " << pos->str(0) << endl;
		  cout << "user name: " << pos->str(1) << endl;
		  cout << "Domain: " << pos->str(2) << endl;
		  cout << endl;
	  }
	  cout << "=============================\n\n";
   }
}



/**************** Regex Token Iterator ******************/
int main() {
	cout << "Hi" << endl;

	//string str = "Apple; Orange, {Cherry}; Blueberry";
	string str = "boq@yahoo.com, boqian@gmail.com; bo@hotmail.com";

	//regex e("[[:punct:]]+");  // 空格,數字,字母以外的可打印字符
	//regex e("[ [:punct:]]+"); 
	regex e("([[:w:]]+)@([[:w:]]+)\.com");
	  
	sregex_token_iterator pos(str.cbegin(), str.cend(), e, 0);    //最后一個參數指定打印匹配結果的哪一部分,0表達整個匹配字符串,1表示第1個子串,-1表示沒有匹配的部分
	sregex_token_iterator end;  // 默認構造定義了past-the-end迭代器
	for (; pos!=end; pos++) {
		cout << "Matched:  " << *pos << endl;
	}
	cout << "=============================\n\n";
		
	
	cin >> str;
}



/**************** regex_replace ******************/
// 將匹配的字符串部分替換
int main() {
	cout << "Hi" << endl;

	string str = "boq@yahoo.com, boqian@gmail.com; bo@hotmail.com";

	regex e("([[:w:]]+)@([[:w:]]+)\.com");
	regex e("([[:w:]]+)@([[:w:]]+)\.com", regex_constants::grep|regex_constants::icase );
	  
	//cout << regex_replace(str, e, "$1 is on $2");
   cout << regex_replace(str, e, "$1 is on $2", regex_constants::format_no_copy|regex_constants::format_first_only);//format_no_copy不匹配的字符部分不拷貝到新串,只匹配第一個
	cout << regex_replace(str, e, "$1 is on $2");    // $1表示第一個子串
		
	std::cin >> str;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM