字符串流的操作---项目遇到的问题。在线等。
下午好,辛苦了。
我现在遇到个问题:
我有个字符串,但字符串中寸的格式是ini文件的格式,
例:
Dim strData As New StringBuilder
strData.Append("[Name]" & vbCrLf)
strData.Append("Test" & vbCrLf)
strData.Append(vbCrLf)
strData.Append("[Content]" & vbCrLf)
strData.Append("I am the original text" & vbCrLf)
strData.Append(vbCrLf)
strData.Append("[Parameter]" & vbCrLf)
strData.Append("A=10 cm" & vbCrLf)
strData.Append("C=20 cm" & vbCrLf)
strData.Append("E=50 cm")
之后我要取得有多少个部分,还有每个部分下有多少个Key,该如何处理比较好。
我现在用的是“[“split,但这样的话,如果这个字符串中出现了“[“就出错了。
请问该如何是好,再现等。。。急。。
问题点数:50、回复次数:10Top
1 楼The123(Shall We Dance? :))回复于 2006-03-02 14:44:40 得分 0
我有个字符串,但字符串中寸的格式是ini文件的格式
-------------
你要是想读取ini文件设置的话就调用windows api就可以了Top
2 楼sunny110(沙漠)回复于 2006-03-02 14:54:44 得分 0
关注Top
3 楼zhanqiangz(闲云野鹤-Overriding)回复于 2006-03-02 15:30:19 得分 0
Dim reg As New RegEx("\b[.*?]\b")
Dim mc As MatchCollection=reg.Match(your string read from ini file)
然后遍历取m.ValueTop
4 楼zhaojie513(赵杰)回复于 2006-03-02 15:42:03 得分 0
Dim reg As New RegEx("\b[.*?]\b")
Dim mc As MatchCollection=reg.Match(your string read from ini file)
然后遍历取m.Value
RegEx在什么类里啊。谢谢了
Top
5 楼zhanqiangz(闲云野鹤-Overriding)回复于 2006-03-02 15:45:09 得分 0
imports System.Text.RegularExpressionsTop
6 楼zhaojie513(赵杰)回复于 2006-03-02 15:46:24 得分 0
Dim reg As New Regex("\b[.*?]\b")
Dim strData As New StringBuilder
strData.Append("[Name]" & vbCrLf)
strData.Append("Test" & vbCrLf)
strData.Append(vbCrLf)
strData.Append("[Content]" & vbCrLf)
strData.Append("I am the original text" & vbCrLf)
strData.Append(vbCrLf)
strData.Append("[Parameter]" & vbCrLf)
strData.Append("A=10 cm" & vbCrLf)
strData.Append("C=20 cm" & vbCrLf)
strData.Append("E=50 cm")
Dim mc As MatchCollection = reg.Match(strData.ToString)报错呀,说不能转换成MatchCollection 类型。。。请教。Top
7 楼zhaojie513(赵杰)回复于 2006-03-02 15:57:20 得分 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim reg As New Regex("\b[.*?]\b")
Dim strData As New StringBuilder
strData.Append("[Name]" & vbCrLf)
strData.Append("Test" & vbCrLf)
strData.Append(vbCrLf)
strData.Append("[Content]" & vbCrLf)
strData.Append("I am the original text" & vbCrLf)
strData.Append(vbCrLf)
strData.Append("[Parameter]" & vbCrLf)
strData.Append("A=10 cm" & vbCrLf)
strData.Append("C=20 cm" & vbCrLf)
strData.Append("E=50 cm")
Dim ary As New ArrayList
Dim MailList As System.Text.RegularExpressions.MatchCollection = reg.Matches(strData.ToString)
MsgBox(MailList .Count)
End Sub
MailList 里没有分解出来呀。请再指教一下。辛苦了。
Top
8 楼zhanqiangz(闲云野鹤-Overriding)回复于 2006-03-02 16:26:46 得分 0
那有可能是我的正则表达式写的不正确,我没有经过测试的。
Dim reg As New Regex("^[.*?]$")
用这个试试
或者用
[.*?]
Top
9 楼zhanqiangz(闲云野鹤-Overriding)回复于 2006-03-02 16:41:05 得分 0
不好意思,我犯了个弱智的错误,因为[]在正则表达式里算是保留关键字,所以应该先转义
string str = "[key]\r\nsssss[name]\r\n";
Regex reg = new Regex(@"\[.*?\]",RegexOptions.Singleline);
MatchCollection m = reg.Matches(str);
foreach (Match mm in m)
{
MessageBox.Show(mm.Value + " " + mm.Index.ToString());
}
}Top
10 楼zhaojie513(赵杰)回复于 2006-03-02 17:07:46 得分 0
Public Function getsectionsname(ByVal striniData As String) As MatchCollection
Dim reg As Regex
Dim sectionList As System.Text.RegularExpressions.MatchCollection
Try
reg = New Regex("\[(?<sectionname>\w*)\]")
sectionList = reg.Matches(striniData)
Return sectionList
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function getsectionstring(ByVal striniData As String, ByVal sectionname As String) As String
sectionname = sectionname.Replace("[", "")
sectionname = sectionname.Replace("]", "")
Dim regexpattern As String = "(\[" + sectionname + "\]" + "(?<SectionString>\w*))"
'Dim regexpattern As String = "(\[Parameter\](?<sectionstring>.*)\[]"
Dim reg As Regex
reg = New Regex(regexpattern, RegexOptions.Singleline)
If reg.IsMatch(striniData) Then
Return reg.Match(striniData).Result("${SectionString}")
End If
Return String.Empty
End Function
getsectionsname 现在可以了。
但取getsectionstring的时候为空是不是表达式不对呀。
辛苦了。Top




