C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: We call the Regex.Split function and pass the pattern \D+ to it. This means one or more non-digit characters.
VB.NET program that uses Regex.Split function
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' The input string.
Dim sentence As String = "10 cats, 20 dogs, 40 fish and 1 programmer."
' Invoke the Regex.Split shared function.
Dim digits() As String = Regex.Split(sentence, "\D+")
' Loop over the elements in the resulting array.
For Each item As String In digits
Console.WriteLine(item)
Next
End Sub
End Module
Output
10
20
40
1
Tip: Treating more than one whitespace as a single whitespace is useful. The pattern is \s+ and it indicates one or more whitespace characters.
VB.NET program that uses Regex.Split function, removes whitespace
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' The input string.
Dim expression As String = "3 * 5 = 15"
' Call Regex.Split.
Dim operands() As String = Regex.Split(expression, "\s+")
' Loop over the elements.
For Each operand As String In operands
Console.WriteLine(operand)
Next
End Sub
End Module
Output
3
*
5
=
15
Note: More examples of Split are available. For simpler requirements, we prefer the String type's implementation.