以文本方式查看主题

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

--  作者:chh2321
--  发布时间:2022/6/29 12:19:00
--  有关查找替换字符串问题
下面这段代码是想把不标准的日期改成标准日期格式(YYYY-MM-DD)
Dim s As String = "2021-4就职百度公司。2021-04-26辞职"
Dim d As String = "(\\d){4}(\\-)(\\d)?(\\d)(?=[^\\x00-\\xff])"
Dim rgx As New System.Text.RegularExpressions.Regex(d)
Dim str As String
For Each match As System.Text.RegularExpressions.Match In rgx.Matches(s) 
    If match.Value.Length = 6 Then
        str = match.Value.Insert(5, "0") & "-15"
    ElseIf match.Value.Length > 6 Then
        str = match.Value & "-15"
    End If
    s = s.Replace(match.Value, str)
Next
MessageBox.Show(s)
如果字符串是"2021-4就职百度公司。2021-04-26辞职",可以改写成"2021-04-15就职百度公司。2021-04-26辞职";
但如果字符串是"2021-04就职百度公司。2021-04-26辞职",就会改写成"2021-04-15就职百度公司。2021-04-15-26辞职"

请教如何处理?

--  作者:有点蓝
--  发布时间:2022/6/29 13:34:00
--  
Dim s As String = "2021-4就职百度公司。2021-04-26辞职"
Dim d As String = "(\\d){4}(\\-)(\\d)?(\\d)(?=[^\\x00-\\xff])"
Dim str = System.Text.RegularExpressions.Regex.Replace(s , d , "$0-15")
Output.Show(str)