3,881
社区成员
发帖
与我相关
我的任务
分享
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
const char *szStr = "S<Hello>A<0>S<Hi>S<aa>";
{
boost::regex reg("[SA]*<[^>]*?>");
boost::cregex_iterator itrBegin(szStr, szStr + strlen(szStr), reg);
boost::cregex_iterator itrEnd;
for (boost::cregex_iterator itr = itrBegin; itr != itrEnd; ++itr) {
// 指向子串对应首位置 指向子串对应尾位置 子串内容
// cout << (*itr)[0].first - szStr << ' ' << (*itr)[0].second - szStr
// << ' ' << *itr << endl;
cout << *itr << endl;
}
}
return 0;
}
Function RegExp(ptn, txt) As String
Dim rtnstr As String
Dim codestr As String
rtnstr = ""
With ScriptControl1
' Set script language (VBScript is the default).
.Language = "VBScript"
' Set UI interaction (TRUE is the default).
.AllowUI = True
' Copy the script to the control.
'--------------------------------------------------------
codestr = ""
codestr = codestr + "Function RegExpTest(patrn, strng) " + vbCrLf
codestr = codestr + " Dim regEx, Match, Matches, RetStr " + vbCrLf
codestr = codestr + " Set regEx = New RegExp " + vbCrLf
codestr = codestr + " regEx.Pattern = patrn " + vbCrLf
codestr = codestr + " regEx.IgnoreCase = True " + vbCrLf
codestr = codestr + " regEx.Global = True " + vbCrLf
codestr = codestr + " Set Matches = regEx.Execute(strng) " + vbCrLf
codestr = codestr + " RetStr="""" " + vbCrLf
codestr = codestr + " For Each Match in Matches " + vbCrLf
codestr = codestr + " RetStr=RetStr+"";""+Match.Value" + vbCrLf
codestr = codestr + " RetStr=RetStr+"",""+Match.SubMatches(0)+"",""+Match.SubMatches(1)" + vbCrLf
codestr = codestr + " Next " + vbCrLf
codestr = codestr + " RegExpTest = RetStr " + vbCrLf
codestr = codestr + " Set regEx = Nothing " + vbCrLf
codestr = codestr + "End Function " + vbCrLf
'--------------------------------------------------------
.AddCode codestr
Dim oMod As Object
Set oMod = .Modules(GlobalModule)
rtnstr = oMod.Run("RegExpTest", ptn, txt)
Set oMod = Nothing
End With
RegExp = rtnstr
End Function
Private Sub Command1_Click()
Debug.Print RegExp("(.*?)<([^>]*?)>", "S<Hello>A<0>S<Hi>S<??>")
' 上面这条语句的运行结果:
' ;S<Hello>,S,Hello;A<0>,A,0;S<Hi>,S,Hi;S<??>,S,??
End Sub
#include "stdafx.h"
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
boost::regex e("(.*?)<([^>]*?)>");
boost::smatch result;
std::string str("S<Hello>A<0>S<Hi>S<aa>");
boost::regex_match(str, result, e);
for (int i = 0; i < result.size(); i++)
{
std::cout<<result[i]<<endl;
}
return 0;
}