Replacing file using regex

后端 未结 2 1550
北海茫月
北海茫月 2021-01-23 16:49

i tried to replace inner text of the month tag i.e month names should be replaced with their specified month number. i tried this,

 Dim strFile As String = File.         


        
相关标签:
2条回答
  • 2021-01-23 17:20

    You could do it like this:

    Dim doc As New XmlDocument()
    Dim months As IDictionary(Of String, String) = New Dictionary(Of String, String)() From {{"January", "1"}, {"February", "2"}, {"March", "3"}, {"April", "4"}, {"May", "5"}, {"June", "6"}, {"July", "7"}, {"8", "August"}, {"September", "9"}, {"October", "10"}, {"November", "11"}, {"December", "12"}}
    Dim expr As New Regex([String].Join("|", months.Keys))
    Dim strFile As String = "May"
    
    doc.Load(TextBox1.Text & "\" & parentFolder & ".xml")
    
    For Each item As XmlNode In doc.GetElementsByTagName("month")
        item.Value = expr.Replace(item.Value, Function(m) months(m.Value))
    Next
    
    0 讨论(0)
  • 2021-01-23 17:22

    Try this

    Dim y = "<conf-start iso-8601-date=""2011-05-31""><day>31</day><month>Jan</month><year>2011</year></conf-start>"
    
    Dim Match = Regex.Match(y, "<month>([^>]*)<\/month>").Groups(1).ToString
    Regex.Replace(y, Match, DateTime.ParseExact(Match, "MMM", CultureInfo.CurrentCulture).Month.ToString)
    

    It will give you OP Like

    <conf-start iso-8601-date="2011-05-31"><day>31</day><month>01</month><year>2011</year></conf-start>
    
    0 讨论(0)
提交回复
热议问题