我试图在两个字符串之间获取所有文本
第一道:<p style="text-align: justify;">
第二个str:</p>
这样的文字:
<p style="text-align: justify;">aaaaa</p><p style="text-align: justify;">ddddd</p><p style="text-align: justify;">bbbbb</p>
我希望输出为
aaaa
dddd
bbbbb
等等
我试过这个功能,但不知道如何循环它..
Public Function GetClosedText(ByVal source As String, ByVal opener As String, ByVal closer As String) As String
Dim indexOfOpener = source.IndexOf(opener)
Dim result As String = ""
If indexOfOpener >= 0 Then ' default is -1 and indices start with 0
indexOfOpener += opener.Length ' now look behind the opener
Dim indexOfCloser = source.IndexOf(closer, indexOfOpener)
If indexOfCloser >= 0 Then
result = source.Substring(indexOfOpener, indexOfCloser - indexOfOpener)
Else
result = source.Substring(indexOfOpener) ' takes the rest behind the opener
End If
End If
Return result
结束功能
答案 0 :(得分:0)
您可以使用正则表达式查找<p>
标记之间的文本。您想要的正则表达式模式应检测>
和<
之间的单词。因此,您可以使用:(\w+)(?=<)(?!>)
?=
表示应该遵循此
?!
表示应该以此
\w+
表示两者之间的一个或多个字母。
这是一个示例代码,用于将每个匹配的文本打印到Console
:
Dim pattern As String = "(\w+)(?=<)(?!>)"
Dim input As String = "<p style=""text-align: justify;"">aaaaa</p><p style=""text-align: justify;"">ddddd</p><p style=""text-align: justify;"">bbbbb</p>"
For Each m As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at index {1}.",
m.Value, m.Index)
Next
答案 1 :(得分:0)
使用正则表达式:<p[^>]*>(?<TEXT>[^<]*)<\/p>
<p[^>]*>
匹配代码中的文字和代码<p >
(?<TEXT>[^<]*)
找到标签之间的字符串,并将其命名为“TEXT”组
<\/p>
匹配转义标记<\p>
Dim input As String = "<p style=""text-align: justify;"">aaaaa</p><p style=""text-align: justify;"">ddddd</p><p style=""text-align: justify;"">bbbbb</p>"
For Each match As Match In Regex.Matches(input, "<p[^>]*>(?<TEXT>[^<]*)<\/p>")
Console.WriteLine(match.Groups("TEXT"))
Next
答案 2 :(得分:0)
如果你想了解字符串操作
Dim xstr As String = "<p style=""text-align: justify;"">aaaaa</p><p style=""text-align: justify;"">ddddd</p><p style=""text-align: justify;"">bbbbb</p>"
Dim temp As String = ""
Dim String1 As String = "<p style=""text-align: justify;"">"
Dim String2 As String = "</p>"
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