regex vb.net 拆分字符串组

ilmyapht  于 2023-06-25  发布在  .NET
关注(0)|答案(2)|浏览(94)

XML文件
这是示例文件,有2个组
(real文件超过1000组)

<Text>You guys were the ones calling
    out and making a racket.</Text>
  
 <Text>I thought you needed help,</Text>

我希望他们这样在vb
如果多于2个组,则将为每个组自动创建新文本框

[textbox1]
You guys were the ones calling
out and making a racket.

[textbox2]
I thought you needed help,

我想我必须通过<Text*>标签来查找文本
并拆分它们,但这是重复的标签,我无法定义它们

g9icjywg

g9icjywg1#

如果是XML,则使用XML。这里我使用了XElement、LINQ和XML文字。希望你能从中得到一些想法。

Dim xe As XElement
    'for production
    'xe = XElement.Load("put path here")

    'for test
    xe = <Subtitles>
             <Subtitle>
                 <Number>1</Number>
                 <Start>00:00:00,000</Start>
                 <End>00:00:02,236</End>
                 <Duration>2,236</Duration>
                 <Text>You guys were the ones calling
out and making a racket.</Text>
             </Subtitle>
             <Subtitle>
                 <Number>2</Number>
                 <Start>00:00:02,238</Start>
                 <End>00:00:03,343</End>
                 <Duration>1,105</Duration>
                 <Text>I thought you needed help,</Text>
             </Subtitle>
         </Subtitles>

    RichTextBox1.Text = (From el In xe...<Number>
                          Where el.Value = "1").FirstOrDefault.Parent.<Text>.Value

    ''or for 2

    'RichTextBox1.Text = (From el In xe...<Number>
    '                      Where el.Value = "2").FirstOrDefault.Parent.<Text>.Value

    RichTextBox1.Clear()
    Dim groups As IEnumerable(Of XElement)
    groups = xe...<Subtitle>

    For Each g As XElement In groups
        Dim s As String
        s = String.Format("Num. {0} Start:{1} End:{2}",
                            g.<Number>.Value,
                            g.<Start>.Value,
                            g.<End>.Value)
        RichTextBox1.AppendText(s)
        RichTextBox1.AppendText(ControlChars.Cr)
    Next
vd8tlhqk

vd8tlhqk2#

我使用这个代码,它工作得很好。现在我可以拆分任何我想拆分的组

Dim xml = XDocument.Load("C:\Users\Administrator\Desktop\12.xml")

Dim xstr As String = xml.ToString
Dim String1 As String = "<Text>"
Dim String2 As String = "</Text>"
Dim xlist As New List(Of String)
Do
    xstr = Mid(xstr, InStr(xstr.ToLower, String1.ToLower) + String1.Length)
    xlist.Add(Mid(xstr, 1, InStr(xstr.ToLower, String2.ToLower) - 1))
    xstr = Mid(xstr, InStr(xstr.ToLower, String2.ToLower) + String2.Length)
Loop Until InStr(xstr.ToLower, String1.ToLower) = 0

Dim i = 0

Do
    RichTextBox1.Text &= xlist(i) & Environment.NewLine
    i += 1
Loop Until i = xlist.Count
  • 以提问者名义发布 *

相关问题