Visual Basic Regular Expression Question

I have a list of strings. When the user enters characters, the program displays all possible lines from the list in the text box.

Dim fruit as new List(Of String) 'contains apple,orange,pear,banana
Dim rx as New Regex(fruit)

For example, if the user enters a, p, l, e, r, then the program displays an apple and a pear. It must match any entry for which all letters have been entered, regardless of order and regardless of additional letters. What should I add to rx? If this is not possible using regular expressions, please indicate any other ways to do this.

+5
source share
1 answer

LINQ Approach

Dim fruits As New List(Of String) From { "apple", "orange", "pear", "banana" }
Dim input As String = "a,p,l,e,r"
Dim letters As String = input.Replace(",", "")
Dim result = fruits.Where(Function(fruit) Not fruit.Except(letters).Any())

Regex Approach:

A regular expression pattern matching the results will look like:

"^[apler]+$"

:

Dim fruits As New List(Of String) From { "apple", "orange", "pear", "banana" }
Dim input As String = "n,a,b,r,o,n,g,e"
Dim letters As String = input.Replace(",", "")
Dim pattern As String = "^[" + letters + "]+$"
Dim query = fruits.Where(Function(fruit) Regex.IsMatch(fruit, pattern))
+7

All Articles