1、先拆分各个项目,参考代码
Dim str As String = "土建100平方米50万元,装修50平方米30万元,安装80万元"
Dim mc = System.Text.RegularExpressions.Regex.Matches(str, "(?<=土建|土建[0-9]+平方米)[0-9]+(?=万元)")
If mc.count > 0 Then
msgbox(mc(0).value)
End If
mc = System.Text.RegularExpressions.Regex.Matches(str, "(?<=装修|装修[0-9]+平方米)[0-9]+(?=万元)")
If mc.count > 0 Then
msgbox(mc(0).value)
End If
mc = System.Text.RegularExpressions.Regex.Matches(str, "(?<=安装|安装[0-9]+平方米)[0-9]+(?=万元)")
If mc.count > 0 Then
msgbox(mc(0).value)
End If
2、写成动态的方式,如
'''
Dim str As String = "土建100平方米50万元,装修50平方米30万元,安装80万元"
For Each s As String In str.split(",")
Dim mc = System.Text.RegularExpressions.Regex.Matches(s, ".+?(?=[0-9]+)")
If mc.count > 0 Then
Dim xm = mc(0).value
msgbox(xm)
mc = System.Text.RegularExpressions.Regex.Matches(str, "(?<=" & xm & "|" & xm & "[0-9]+平方米)[0-9]+(?=万元)")
If mc.count > 0 Then
msgbox(mc(0).value)
End If
End If
Next
3、动态生成表,统计数据
'''
Dim dic2 As new Dictionary(of String, object)
Dim xms As new List(of String)
For Each dr As DataRow In DataTables("表A").Select("年份 is not null")
Dim str As String = dr("项目规模")
Dim year As String = cdate(dr("年份")).year
If dic2.ContainsKey(year) = False Then
Dim dic As new Dictionary(of String, Double)
dic2.Add(year, dic)
End If
Dim dic1 As Dictionary(of String, Double) = dic2(year)
For Each s As String In str.split(",")
Dim mc = System.Text.RegularExpressions.Regex.Matches(s, ".+?(?=[0-9]+)")
If mc.count > 0 Then
Dim xm = mc(0).value
If xms.Contains(xm) = False Then
xms.add(xm)
End If
mc = System.Text.RegularExpressions.Regex.Matches(str, "(?<=" & xm & "|" & xm & "[0-9]+平方米)[0-9]+(?=万元)")
If mc.count > 0 Then
If dic1.ContainsKey(xm) = False Then
dic1.Add(xm,mc(0).value)
Else
dic1(xm) += mc(0).value
End If
End If
End If
Next
Next
Dim dtb As New DataTableBuilder("统计")
dtb.AddDef("年份", Gettype(String), 32)
For Each xm As String In xms
dtb.AddDef(xm, Gettype(Double))
Next
dtb.Build()
Dim t As Table = Tables("统计")
For Each key As object In dic2.Keys
Dim nr As Row = t.addnew
nr("年份") = key
For Each xm As String In xms
nr(xm) = dic2(key)(xm)
Next
Next
MainTable=t
[此贴子已经被作者于2018/11/14 17:40:30编辑过]