正则表达式如何重复匹配?
比如我的格式是:
标识<内容>
S<Hello>A<0>S<Hi>S<??>
我写的正则是:([SA]<[^<>]*>)*
但就是不能匹配成
S<Hello>
A<0>
S<Hi>
S<??>
请问这种正则应该怎么写?
[解决办法]
初步判断是贪婪量词的问题,建议试一下
([SA]<[^<>]*?>)*
[解决办法]
正则式最后的*不需要吧
[解决办法]
字符串:S<Hello>A<0>S<Hi>S<??>
正则表达式:(.*?)<([^>]*?)>
匹配结果:
1.S<Hello>
(1).S (2).Hello
2.A<0>
(1).A (2).0
3.S<Hi>
(1).S (2).Hi
4.S<??>
(1).S (2).??
推荐使用regtest软件
[解决办法]
正则表达式速查 正则表达式举例 正则表达式学习 (4页A4纸)
http://download.csdn.net/detail/zhao4zhong1/1808549
[解决办法]
boost的正则表达式可能和regtest软件用的正则表达式在正则表达式语法上有细微区别。
[解决办法]
用VBSCript 在msscript.ocx控件中验证:
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 <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;
}