Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何获取字符串并将其添加到列表中?_Regex_Vb.net - Fatal编程技术网

Regex 如何获取字符串并将其添加到列表中?

Regex 如何获取字符串并将其添加到列表中?,regex,vb.net,Regex,Vb.net,要加载的文件是xml文件。在我到达特定路径后,我想逐行获取特定字符串,然后将其添加到列表中。 下面是我想从文件中提取的字符串示例。(以粗体文本突出显示 <XMLTAG> <p><b>[1]</b> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from

要加载的文件是xml文件。在我到达特定路径后,我想逐行获取特定字符串,然后将其添加到列表中。 下面是我想从文件中提取的字符串示例。(以粗体文本突出显示

<XMLTAG>

<p><b>[1]</b> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum <LINK HREF="test"><i>NAME</i> [2017] 3 ABCD 247</LINK> [1234] 1 ABC 123; [1234] 1 ABC 123:</p>

<p><b>[2]</b> <LINK HREF="test"><i>NAME</i> [2017] 3 ABCD 247</LINK> [1234] 1 ABC 123 lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>


</XMLTAG>


<> P>不过,我一直坚持如何在我的代码中实现它,任何人都可以在这方面帮助我吗?谢谢。

< P>请考虑下面关于ReXEX的代码片段,它可以帮助您解决问题。

私有函数Slist(ByVal list As list(Of String))As list(Of String)
Dim rlist作为新列表(字符串)
Dim rx作为新的System.Text.RegularExpressions.Regex(\[\d{4}\]\s\d{1,3}\s\w{3}\s\d{1,3}”)
将每个项目作为列表中的字符串
如果rx.IsMatch(项目)则
rlist.Add(rx.Match(item).Value)
如果结束
下一个
返回rlist.Distinct.ToList
端函数
祝你好运


编辑1

让我们把事情弄清楚。如果你喜欢ReX,请考虑以下内容:

Dim _slist As IEnumerable(Of String) = Slist(IO.File.ReadAllLines("your-path-here.xml"))
假设您有以下几行:

Dim s1 As String=“[1]与普遍的看法相反,Lorem Ipsum并非简单的随机文本。它起源于公元前45年的一段古典拉丁文学,距今已有2000多年的历史。弗吉尼亚州汉普顿悉尼学院的拉丁语教授理查德·麦克林托克(Richard McClintock)从Lorem Ipsum的名字[2017]3 ABCD 247[1234]中查找到了一个更为晦涩的拉丁语单词Concertetur1 ABC 123;[4567]2 DEF 456:

Dim s2 As String=“[2]名称[2017]3 ABCD 247[1234]1 ABC 123 lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,lorem Ipsum一直是印刷行业的标准虚拟文本,当时一家不知名的印刷商拿起一个打印工具,争先恐后地将其制成一本字体样本书。它不仅存活了五个世纪,而且还跨越了电子排版,rema基本不变。

如果您重新注册模式:

Dim pat1 As String = "\[\d+\]\s\d\s\D{3}\s\d+"

For Each m As Match In Regex.Matches(s1, pat1)
    Console.WriteLine(m.Value)
Next

For Each m As Match In Regex.Matches(s2, pat1)
    Console.WriteLine(m.Value)
Next
Dim pat2 As String = "(\[\d+\]\s\d\s\D{3}\s\d+.\s\[\d+\]\s\d\s\D{3}\s\d+)"

For Each m As Match In Regex.Matches(s1, pat2)
    Console.WriteLine(m.Value)
Next

For Each m As Match In Regex.Matches(s2, pat2)
    Console.WriteLine(m.Value)
Next
输出将是两个字符串(s1和s2)的3个匹配项:

[1234]1 ABC 123
[4567]2 DEF 456
[1234]1 ABC 123

鉴于模式的输出:

Dim pat1 As String = "\[\d+\]\s\d\s\D{3}\s\d+"

For Each m As Match In Regex.Matches(s1, pat1)
    Console.WriteLine(m.Value)
Next

For Each m As Match In Regex.Matches(s2, pat1)
    Console.WriteLine(m.Value)
Next
Dim pat2 As String = "(\[\d+\]\s\d\s\D{3}\s\d+.\s\[\d+\]\s\d\s\D{3}\s\d+)"

For Each m As Match In Regex.Matches(s1, pat2)
    Console.WriteLine(m.Value)
Next

For Each m As Match In Regex.Matches(s2, pat2)
    Console.WriteLine(m.Value)
Next
将仅与第一个字符串(s1)匹配一次:

[1234]1 ABC 123;[4567]2 DEF 456


希望现在一切都清楚了,祝你好运。

你的XML格式不正确,但这里不需要正则表达式。你基本上是要求在最后一次出现关键字
之后获取所有文本。话虽如此,这里有一个函数可以做到:

Private Function Slist(ByVal lines() As String) As IEnumerable(Of String)
    Return
        From line As String in lines
        Where line.IndexOf("</LINK>") > -1
        Select line.Substring(line.Substring(line.LastIndexOf("</LINK>") + 8))
End Function

看起来您想要的是…块之外的所有内容?您是否将其作为文件读入?是的,但在这些字符串模式中只有
[1234]1 ABC 123;[1234]1 ABC 123
@LarsTechyes,xml file@MarkHall
\s+(\[\d+\]\s\d\s\d+\s+.\s\[\d+\]\s\d\s\d+\d+\s\d+
除了这些模式之外还有另一个文本
[1234]1 ABC 123;(1234)1 ABC 123 < /代码>我认为正则表达式的原因是我需要得到这些模式,但是它必须在@ ARI之后——你能提供一个更准确的数据表示吗?这个文本只考虑这些字符串模式<代码> [1234 ] 1 abc 123;[1234 ] 1 ABC 123 < /代码>??没有伙伴,它返回[1234 ]1仅ABC 123或此模式中的任何内容。例如:[5426]3 DAW 323.我刚才给了你一个关于如何使用你在问题中提到的模式来使用正则表达式的例子。如果我是你,我会按照@David的答案,除非你有包含很多的条目。你的代码在之后检测到模式了吗?或者它检测到了文件中的任何模式。我使用了你在这里教授的一些指导,这就是声明n个字符串和用于显示数据的For-Each循环。文本现在根据regex模式显示。无论如何,我将它们存储到字符串列表中,这样操作数据就很容易了。无论如何,非常感谢您的精彩解释!非常感谢@JQSOFT