以文本方式查看主题

-  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=161533)

--  作者:chh2321
--  发布时间:2021/3/21 14:02:00
--  如何用正则表达式将匹配到的不标准日期格式化成标准日期,且在转换后在每个标准日期前插入一个回车符,并替换原来文本?
请教老师:
     如何用正则表达式将匹配到的不标准日期格式化成标准日期,且在转换后在每个标准日期前插入一个回车符,并替换原来文本。

     比如:
           2018年5月毕业于西安大学。2018/9就职于东方公司,2020-7-16在东方公司任销售经理,2021/5/28任销售总监。

     匹配、转换成如下文本:
           2018-05-01毕业于西安大学。
           2018-09-01就职于东方公司,
           2020-07-16在东方公司任销售经理,
           2021-05-28任销售总监。

代码如下:
Dim str As String = "2018年5月毕业于西安大学。2018/9就职于东方公司,2020-7-16在东方公司任销售经理,2021/5/28任销......"
Dim reg As New System.Text.RegularExpressions.Regex("(^[0-9]{2,4}[-|/|.|年]\\d{1,2}[-|/|.|月]\\d{0,2}[日]?)")
If str Is Nothing Then
    Return
Else
    For Each mat As System.Text.RegularExpressions.Match In reg.Matches(str)
        ?????????????
    Next
End If

代码不会写了,请老师指点,谢谢!
    

--  作者:hongsejiyi
--  发布时间:2021/3/21 21:41:00
--  

此主题相关图片如下:2021-03-21_213839.png
按此在新窗口浏览图片
以下内容为程序代码:

1 Dim str As String = "2018年5月毕业于西安大学.2018/9就职于东方公司,2020-7-16在东方公司任销售经理,2021/5/28任销......"
2 Dim reg As New System.Text.RegularExpressions.Regex("(^[0-9]{2,4}[-|/|.|年]\\d{1,2}[-|/|.|月]\\d{0,2}[日]?)")
3 Dim dm As String ="[0-9]{2,4}[年|\\/|\\-|\\.]?\\d{1,2}[月|\\/|\\-|\\.]\\d{0,2}[日]?[\\u4e00-\\u9fa5]+[.|,]+"
4 Dim dm1 As String ="[0-9]{2,4}[年|\\/|\\-|\\.]?\\d{1,2}[月|\\/|\\-|\\.]\\d{0,2}[日]?"
5 Dim mc = System.Text.RegularExpressions.Regex.Matches(str,dm)
6 Dim nr,nr1,nr2 As String
7
8 For i As Integer =0 To mc.count-1
9 nr =mc(i).value
10 Dim mc1 = System.Text.RegularExpressions.regex.match(nr,dm1)
11 nr1=mc1.value
12 nr1=nr1.Replace("年","-").Replace("月","-").Replace("日","").Replace(".","-").Replace("/","-")
13 If not nr1.EndsWith("-") Then
14 nr1=nr1+"-"
15 End If
16 nr2=""
17 For Each s As String In nr1.Split("-")
18 Select Case s.Length
19 Case 4
20 nr2=nr2+format(cint(s),"0000")+"-"
21 Case 1,2
22 nr2=nr2+format(cint(s),"00")+"-"
23 Case 0
24 nr2=nr2+"01"
25 End Select
26
27 Next
28 If nr2.Length>10 Then
29 nr2=nr2.SubString(0,10)
30 End If
31 output.Show(nr2 & "," & nr.Replace(mc1.value,""))
32 Next

 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:zhengze.txt


[此贴子已经被作者于2021/3/21 21:42:57编辑过]

--  作者:有点蓝
--  发布时间:2021/3/21 21:51:00
--  
dim ss as string = str
    For Each mat As System.Text.RegularExpressions.Match In reg.Matches(str)
        ss = ss.replace(mat.value,"|" & mat.value)
    Next
dim arr() as string = ss.split("|")
    For Each s As String In arr
        msgbox(s)
    Next