Regex 拆分除符号以外的字符串
我尝试使用以下两种代码:Regex 拆分除符号以外的字符串,regex,vb.net,string,Regex,Vb.net,String,我尝试使用以下两种代码: Dim splitQuery() As String = Regex.Split(TextBoxQuery.Text, "\s+") 及 我的示例查询是a dog。注意dog和之间只有一个空格。当我检查splitQuery的长度时,它给了我3,拆分的单词是a,dog和 如何阻止它将和其他符号作为单词计数?我只希望单词/术语(字母数字)存储在我的splitQuery数组中。谢谢。您还应该能够创建一个不需要的字符字符串,并使用StringSplit选项
Dim splitQuery() As String = Regex.Split(TextBoxQuery.Text, "\s+")
及
我的示例查询是a dog。
注意dog
和
之间只有一个空格。当我检查splitQuery的长度时,它给了我3,拆分的单词是a
,dog
和
如何阻止它将
和其他符号作为单词计数?我只希望单词/术语(字母数字)存储在我的splitQuery数组中。谢谢。您还应该能够创建一个不需要的字符字符串,并使用StringSplit选项对其进行修剪,以删除空输入
dim unwanted as string = "./?!#"
Dim splitQuery() as string = yourString.Trim(unwanted.tochararray).Split(New Char() {" "c}), StringSplitOptions.RemoveEmptyEntries)
您还应该能够创建一个由不需要的字符组成的字符串,并使用StringSplit选项对其进行修剪,以删除mptyEntries
dim unwanted as string = "./?!#"
Dim splitQuery() as string = yourString.Trim(unwanted.tochararray).Split(New Char() {" "c}), StringSplitOptions.RemoveEmptyEntries)
我将分两部分来解决这个问题
Imports System.Collections
' ... Your Other Code ...
' A function to determine if a string is AlphaNumeric
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
Dim IsAlpha As Boolean = False
If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$") Then
IsAlpha = True
Else
IsAlpha = False
End If
Return IsAlpha
End Function
' A function to get the words from the textbox
Private Function GetWords() As String()
' Get a raw list of all words separated by spaces
Dim splitQuery() As String = Regex.Split(TextBoxQuery.Text, "\s+")
' ArrayList to place all words into:
Dim alWords As New ArrayList()
' Loop all words and check them:
For Each word As String In splitQuery
If(IsAlphaNum(word)) Then
' Word is alphanumeric
' Add it to the list of alphanumeric words
alWords.add(word)
End If
Next
' Convert the ArrayList of words to a primitive array of strings
Dim words As String() = CType(alWords.ToArray(GetType(String)), String())
' Return the list of filtered words
return words
End Function
此代码执行以下操作:
ArrayList
ArrayList
。如果不是字母数字,则忽略该术语ArrayList
中的术语/单词转换回普通字符串数组并返回由于此解决方案使用
ArrayList
,因此需要导入System.Collections
。我将分两部分解决此问题
Imports System.Collections
' ... Your Other Code ...
' A function to determine if a string is AlphaNumeric
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
Dim IsAlpha As Boolean = False
If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$") Then
IsAlpha = True
Else
IsAlpha = False
End If
Return IsAlpha
End Function
' A function to get the words from the textbox
Private Function GetWords() As String()
' Get a raw list of all words separated by spaces
Dim splitQuery() As String = Regex.Split(TextBoxQuery.Text, "\s+")
' ArrayList to place all words into:
Dim alWords As New ArrayList()
' Loop all words and check them:
For Each word As String In splitQuery
If(IsAlphaNum(word)) Then
' Word is alphanumeric
' Add it to the list of alphanumeric words
alWords.add(word)
End If
Next
' Convert the ArrayList of words to a primitive array of strings
Dim words As String() = CType(alWords.ToArray(GetType(String)), String())
' Return the list of filtered words
return words
End Function
此代码执行以下操作:
ArrayList
ArrayList
。如果不是字母数字,则忽略该术语ArrayList
中的术语/单词转换回普通字符串数组并返回由于此解决方案使用
ArrayList
,因此需要导入System.Collections
。我建议分两步执行:
- 使用
从字符串末尾删除非单词字符txt=Regex.Replace(TextBoxQuery.Text,“\W+$”,“”,RegexOptions.RightToLeft)
- 然后,使用
进行拆分:\s+
splits=Regex.split(txt,“\s+”)
splits = Regex.Split(Regex.Replace(TextBoxQuery.Text, "^\W+|\W+$", ""), "\W+")
这里,
Regex.Replace(TextBoxQuery.Text,“^\W+\W+$”,”)
删除字符串开头和结尾的非单词字符。我建议分两步执行:
- 使用
从字符串末尾删除非单词字符txt=Regex.Replace(TextBoxQuery.Text,“\W+$”,“”,RegexOptions.RightToLeft)
- 然后,使用
进行拆分:\s+
splits=Regex.split(txt,“\s+”)
splits = Regex.Split(Regex.Replace(TextBoxQuery.Text, "^\W+|\W+$", ""), "\W+")
此处,
Regex.Replace(TextBoxQuery.Text,“^\W+\W+$”,”)
删除字符串开头和结尾的非单词字符。是否要从结尾的非单词字符中修剪字符串?只需使用Regex.Replace(str,“\W*$”,“”,RegexOptions.RightToLeft)
,然后用\s+
拆分即可。对您有用吗?这是最有效的答案。非常感谢你!是否要从末尾的非单词字符中修剪字符串?只需使用Regex.Replace(str,“\W*$”,“”,RegexOptions.RightToLeft)
,然后用\s+
拆分即可。对您有用吗?这是最有效的答案。非常感谢你@我想斯特里比雪夫的答案更短。不过,谢谢你给我另一个想法@我想斯特里比雪夫的答案更短。不过,谢谢你给我另一个想法!我想列出所有不需要的字符,特别是所有的符号需要时间,我可能会忘记一些。我仍然感谢你的回答。我可以把它应用于其他情况。谢谢。我想列出所有不需要的字符,特别是所有的符号需要时间,我可能会忘记一些。我仍然感谢你的回答。我可以把它应用于其他情况。谢谢