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+
    拆分即可。对您有用吗?这是最有效的答案。非常感谢你@我想斯特里比雪夫的答案更短。不过,谢谢你给我另一个想法@我想斯特里比雪夫的答案更短。不过,谢谢你给我另一个想法!我想列出所有不需要的字符,特别是所有的符号需要时间,我可能会忘记一些。我仍然感谢你的回答。我可以把它应用于其他情况。谢谢。我想列出所有不需要的字符,特别是所有的符号需要时间,我可能会忘记一些。我仍然感谢你的回答。我可以把它应用于其他情况。谢谢