以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]如何用正则表达式从某一列提取特定数字到另一列  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=97638)

--  作者:chh2321
--  发布时间:2017/3/16 0:41:00
--  [求助]如何用正则表达式从某一列提取特定数字到另一列
 老师,请教:

如何用正则表达式从一列提取特定数字到另一列。例如:

第一列                                 第二列             第三列
...较前相比有明显好转,         45×34             36×23
原约45×34mm,现为
36×23mm



老师能否给写一段示例代码,谢谢!

--  作者:有点色
--  发布时间:2017/3/16 1:38:00
--  

参考代码

 

 

Dim str As String = "原约45×34mm,现为36×23mm"
Dim mc = System.Text.RegularExpressions.Regex.Matches(str, "[0-9.]+×[0-9.]+")
msgbox(mc(0).value)
msgbox(mc(1).value)

--  作者:chh2321
--  发布时间:2017/3/16 10:19:00
--  
 
谢谢老师,那么晚还回复!

--  作者:chh2321
--  发布时间:2017/3/16 16:42:00
--  

老师,我写了代码,还有点问题,请帮我看看。
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:测试代码.foxdb


遇到的问题写在表B的DataColChanged事件中

--  作者:有点色
--  发布时间:2017/3/16 16:44:00
--  
无法直接打开你的项目。报什么错?贴出你写的代码。
--  作者:chh2321
--  发布时间:2017/3/16 17:25:00
--  
Select Case e.DataCol.Name
    Case "诊疗经过"
        If e.DataRow.isnull("诊疗经过") Then
            e.DataRow("原大小") = Nothing
            e.DataRow("现大小") = Nothing
        Else
            Dim str As String = e.DataRow("诊疗经过")
            Dim mc1 = System.Text.RegularExpressions.Regex.Matches(str,"(原[\\u4e00-\\u9fa5]+)([0-9.]+[×*x][0-9.]+)(mm|cm)")
            Dim mc2 = System.Text.RegularExpressions.Regex.Matches(str,"(现[\\u4e00-\\u9fa5]+)([0-9.]+[×*x][0-9.]+)(mm|cm)")
            If mc1 IsNot Nothing Then
                e.DataRow("原大小") = mc1(0).value
            Else 
                e.DataRow("原大小") = Nothing
            End If
            If mc2 IsNot Nothing Then
                e.DataRow("现大小") = mc2(0).value
            Else 
                e.DataRow("现大小") = Nothing
            End If
        End If
End Select


1、现在的问题是,当无数据可提取时,程序会出错,如何修改代码?

2、结果是"原..35×21mm",如果我想只得到"35×21",正则表达式如何写?

3、如果我想只获取与肝有关的大小,可以吗?

--  作者:chh2321
--  发布时间:2017/3/16 17:28:00
--  
图片点击可在新窗口打开查看

--  作者:有点色
--  发布时间:2017/3/16 17:51:00
--  
Select Case e.DataCol.Name
    Case "诊疗经过"
        If e.DataRow.isnull("诊疗经过") Then
            e.DataRow("原大小") = Nothing
            e.DataRow("现大小") = Nothing
        Else
            Dim str As String = e.DataRow("诊疗经过")
            Dim mc1 = System.Text.RegularExpressions.Regex.Matches(str,"(?<=原[\\u4e00-\\u9fa5]+)([0-9.]+[×*x][0-9.]+)(?=mm|cm)")
            Dim mc2 = System.Text.RegularExpressions.Regex.Matches(str,"(?<=现[\\u4e00-\\u9fa5]+)([0-9.]+[×*x][0-9.]+)(?=mm|cm)")
            If mc1.count > 0 Then
                e.DataRow("原大小") = mc1(0).value
            Else
                e.DataRow("原大小") = Nothing
            End If
            If mc2.count > 0 Then
                e.DataRow("现大小") = mc2(0).value
            Else
                e.DataRow("现大小") = Nothing
            End If
        End If
End Select

--  作者:chh2321
--  发布时间:2017/3/16 18:57:00
--  
 

谢谢老师!