greta一些简单实用的字符串匹配
生活随笔
收集整理的這篇文章主要介紹了
greta一些简单实用的字符串匹配
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼 #if?0//整數
????match_results?results;
????tstring?str("f34");
????rpattern?pat("^[+|-]?\\d+\\d*$");
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
#if?0//正整數
????match_results?results;
????tstring?str("+323f444");
????rpattern?pat("^[+]?\\d+\\d*$");
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
#if?0//浮點數
????match_results?results;
????tstring?str("-3.23444");
????rpattern?pat("^[+|-]?[0-9]+\\.?[0-9]+$");//^\-?[0-9]*\.?[0-9]*$
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
#if?0//字母
????match_results?results;
????tstring?str(".asdf");
????rpattern?pat("^[a-z]*$");//^\-?[0-9]*\.?[0-9]*$
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
????subst_results?results;
????tstring?str("a43sdf");
????REGEX_FLAGS?dw?=?GLOBAL?|?NOCASE;
????rpattern?pat("[0-9]","d",dw);
????int?cnts?=?pat.substitute(str,results);
????subst_results::backref_vector?vec?=?results.all_backrefs();
????subst_results::backref_vector::iterator?iter;
????std::string?m_strResult?=?"";
????for(?iter?=?vec.begin();?iter?!=?vec.end();?iter++?)
????{
????????string?_str?=?(*iter).str();
????????m_strResult?+=?_str.c_str();
????}
#endif
#if?0//count?計算正則表達式在串中出現的次數
????tstring?str("asdf");
????rpattern?pat("[a-z]");
????int?cnts?=?pat.count(str);
#endif
#if?0//split?用正則表達式作為分隔符來切分串
????split_results?results;
????tstring?str("asdf");
????rpattern?pat("[a-z]");
????int?cnts?=?pat.split(str,?results);
#endif
????match_results?results;
????tstring?str("f34");
????rpattern?pat("^[+|-]?\\d+\\d*$");
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
#if?0//正整數
????match_results?results;
????tstring?str("+323f444");
????rpattern?pat("^[+]?\\d+\\d*$");
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
#if?0//浮點數
????match_results?results;
????tstring?str("-3.23444");
????rpattern?pat("^[+|-]?[0-9]+\\.?[0-9]+$");//^\-?[0-9]*\.?[0-9]*$
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
#if?0//字母
????match_results?results;
????tstring?str(".asdf");
????rpattern?pat("^[a-z]*$");//^\-?[0-9]*\.?[0-9]*$
????match_results::backref_type?br?=?pat.match(str,?results);
????if?(br.matched)
????{
????????tcout?<<?"match"?<<?std::endl;
????}
????else
????{
????????tcout?<<?"not?match"?<<?std::endl;
????}
#endif
?其他一些實用代碼
?
代碼 #if?1//subsitute?替換????subst_results?results;
????tstring?str("a43sdf");
????REGEX_FLAGS?dw?=?GLOBAL?|?NOCASE;
????rpattern?pat("[0-9]","d",dw);
????int?cnts?=?pat.substitute(str,results);
????subst_results::backref_vector?vec?=?results.all_backrefs();
????subst_results::backref_vector::iterator?iter;
????std::string?m_strResult?=?"";
????for(?iter?=?vec.begin();?iter?!=?vec.end();?iter++?)
????{
????????string?_str?=?(*iter).str();
????????m_strResult?+=?_str.c_str();
????}
#endif
#if?0//count?計算正則表達式在串中出現的次數
????tstring?str("asdf");
????rpattern?pat("[a-z]");
????int?cnts?=?pat.count(str);
#endif
#if?0//split?用正則表達式作為分隔符來切分串
????split_results?results;
????tstring?str("asdf");
????rpattern?pat("[a-z]");
????int?cnts?=?pat.split(str,?results);
#endif
?
?
轉載于:https://www.cnblogs.com/lancidie/archive/2011/02/09/1950222.html
總結
以上是生活随笔為你收集整理的greta一些简单实用的字符串匹配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 向日葵远程控制app怎么用手机控制电脑(
- 下一篇: 跑跑卡丁车手游车辆升级技巧