.net 使用正则表达式替换文件
我试图替换月份标记的内部文本,即月份名称应替换为其指定的月份编号。 我试过这个.net 使用正则表达式替换文件,.net,regex,vb.net,visual-studio-2010,.net,Regex,Vb.net,Visual Studio 2010,我试图替换月份标记的内部文本,即月份名称应替换为其指定的月份编号。 我试过这个 Dim strFile As String = File.ReadAllText(TextBox1.Text & "\" & parentFolder & ".xml") strFile = Regex.Replace(strFile, "<conf-start iso-8601-date=""([0-9-]+)""><day>([0-9]+)</day
Dim strFile As String = File.ReadAllText(TextBox1.Text & "\" & parentFolder & ".xml")
strFile = Regex.Replace(strFile, "<conf-start iso-8601-date=""([0-9-]+)""><day>([0-9]+)</day><month>March</month>", "<conf-start iso-8601-date=""([0-9-]+)""><day>([0-9]+)</day><month>03</month>")
File.WriteAllText(TextBox1.Text & "\" & parentFolder & ".xml", strFile)
Dim strFile As String=File.ReadAllText(TextBox1.Text&“\”&parentFolder&“.xml”)
strFile=Regex.Replace(strFile,“([0-9]+)March”,“([0-9]+)03”)
File.writealText(TextBox1.Text&“\”&parentFolder&“.xml”,strFile)
现在的问题是如果线路是这样的
<conf-start iso-8601-date="2011-03-06"><day>06</day><month>March</month><year>2011</year></conf-start>
2011年3月6日
在这里,上面的表达式捕捉数据并用
<conf-start iso-8601-date=""([0-9-]+)""><day>([0-9-]+)</day><month>03</month><year>2011</year></conf-start>
([0-9-]+)032011
相反,它应该取代
<conf-start iso-8601-date="2011-03-06"><day>06</day><month>03</month>
0603
任何帮助都将非常感谢试试这个
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)
Dim y=“2011年1月31日”
Dim Match=Regex.Match(y,“([^>]*)”)”.Groups(1).ToString
Regex.Replace(y,Match,DateTime.ParseExact(Match,“MMM”,CultureInfo.CurrentCulture).Month.ToString)
它会给你类似的机会
<conf-start iso-8601-date="2011-05-31"><day>31</day><month>01</month><year>2011</year></conf-start>
31012011
您可以这样做:
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
这里的问题是,该文件是无效的xml文件,我必须对该特定文件进行更改。它向我显示了一个错误,无法找到文件……。/confjats.dtdisnt可以对我的代码进行某些更改并修复错误problem@jigar这可能有助于: