[英]vb.net how to check if a string has a certain word
i want to check if a string matches a word in array not just matches characters. 我想检查一个字符串是否与数组中的单词匹配,而不仅仅是匹配字符。 the contains method just checks the matches characters not the whole word.
contains方法只检查匹配字符而不是整个单词。 here is my code:
这是我的代码:
Dim builder As New StringBuilder()
Dim reader As New StringReader(txtOCR.Text)
Dim titles() As String = {"the", "a", "an", "of"}
Dim regex As New Regex(String.Join("|", titles), RegexOptions.IgnoreCase)
While True
Dim line As String = reader.ReadLine()
If line Is Nothing Then Exit While
Dim WordCount = New Regex("\w+").Matches(line).Count
If WordCount = 1 And Not line.ToLower().Contains("by") Then
builder.AppendLine(line)
ElseIf regex.IsMatch(line) Then
builder.AppendLine(line)
End If
End While
txtTitle.Text = builder.ToString()
您可以使用单词边界\\b
来包围单个单词:
New Regex("\b" & String.Join("\b|\b", titles) & "\b")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.