Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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
vb.net如何拆分字符串_Vb.net - Fatal编程技术网

vb.net如何拆分字符串

vb.net如何拆分字符串,vb.net,Vb.net,我有一个文件txt文件,该文件的值在值“accession\u number=”之后和值“&token”之前拆分 文本文件中的示例值: 10.0.0.6:80/ImageSuite/Web/Worklist/DICOMViewer.aspx?患者id=5049885和研究uid=201702060824和登录号=20170206082802和令牌 10.0.0.6:80/ImageSuite/Web/Worklist/DICOMViewer.aspx?患者id=4409276和研究uid=201

我有一个文件txt文件,该文件的值在值“accession\u number=”之后和值“&token”之前拆分

文本文件中的示例值:

10.0.0.6:80/ImageSuite/Web/Worklist/DICOMViewer.aspx?患者id=5049885和研究uid=201702060824和登录号=20170206082802和令牌

10.0.0.6:80/ImageSuite/Web/Worklist/DICOMViewer.aspx?患者id=4409276和研究uid=201702060826和登录号=20170206083002和令牌

10.0.0.6:80/ImageSuite/Web/Worklist/DICOMViewer.aspx?患者id=4402764和研究uid=201702060801和登录号=20170206080416和令牌

10.0.0.6:80/ImageSuite/Web/Worklist/DICOMViewer.aspx?患者id=4402537和研究uid=201702060837和登录号=20170206084025和令牌

处理后的示例值:
20170206082802
20170206083002
20170206080416
20170206084025


谢谢

您可以读取所有行,然后根据这些行执行以下代码(假设您要读取的文件位于C:\test.txt)

这将使用所有行作为输入,然后对于非空的行,将使用
&
进行拆分,然后检查该字段是否以登录号开始,如果是,则按
=
进行拆分以返回数组中的第二项

作为额外解释:

from line in File.ReadAllLines("C:\test.txt")
同时计算文件中的每一行

' input eg: 10.0.0.6:80/ImageSuite/Web/Worklist/DICOMViewer.aspx?patient_id=5049885&study_uid=201702060824&accession_number=20170206082802&token
where not String.IsNullOrWhiteSpace( line )
将排除所有空行(或仅存在空白的行)

将使用
&
作为分隔符将找到的每一行拆分为字符串数组

 ' field eg: accession_number=20170206082802
 where field.StartsWith("accession_number=")
将排除所有不以
登录号=

 select field.Split("=")(1)
 ' result sample: 20170206082802
将在
=


这是一个完整的例子。它使用一种稍微不同的方法从流中读取,只是因为在那种环境中,我不能提供虚拟文件,但是,它应该做到这一点。

您可以使用将每一行读取到字符串数组中,然后使用

”声明正则表达式。

Dim Parser作为新的正则表达式(“(?当您可以使用其中一个或迭代时,为什么要重新发明轮子?@VisualIncent这是真的,并没有真正考虑到这一点:)然而,这并不是问题的一部分,我只是添加了小提琴作为一个完整的示例。不过,我现在更新了示例。此外,您的示例也做了类似的事情,在我的示例中,他只需从流的来源处替换。尽管我可能想得太多了;)我知道我的方法也做了类似的事情,但我主要提供了一种替代方法。--我写了前面的评论,因为你可以通过使用内置方法来节省一些代码和循环,而不是编写基本上相同的新方法。虽然这不是问题的一部分,但利用良好的实践仍然是一个很好的答案(不是说你在做什么一定不好,但你的部分代码可以用内置方法替换:)@VisualIncent当然,这是个好主意,我在回答中更新了代码,并指出了我为什么在dotnetfiddle中使用不同的方法
 ' field eg: accession_number=20170206082802
 where field.StartsWith("accession_number=")
 select field.Split("=")(1)
 ' result sample: 20170206082802
'Declare the Regex.
Dim Parser As New Regex("(?<=accession_number\=)\d+", RegexOptions.IgnoreCase)

'Read the file's lines into an array of strings.
Dim Lines As String() = File.ReadAllLines("C:\test.txt")

'Iterate over the array.
For Each Line As String In Lines
    Dim m As Match = Parser.Match(Line) 'Match the pattern.
    If m.Success = True Then 'Was the match successful?
        Console.WriteLine(m.Value) 'Print the matched value.
    End If
Next