Using Like operator in the Select Case statement
Select Case sProductName
....Case Is Like "* tea *", Is Like "* coffee *"
....Case Like "* beer *", Like "* wine *" ' the Is keyword could be optional
Great suggestion. As you’ve noted in all of your suggestions there’s a lot more potential with the Select Case statement that we don’t yet support. Reference equality comparisons (Is/IsNot) and the Like operator are great examples as is type-checking. We’re actively thinking about improving the Select Case statement for these scenarios.
Your suggestion is actually the most likely syntax we would use if we were to implement this feature and it seems more like a bug that it doesn’t just work. That said is the Like operator still useful to you compared to say RegEx – which is more powerful?
-Anthony D. Green, Program Manager, Visual Basic & C# Languages Team
Eric Lamey commented
Add linq/lambda too
Select Case = From c as customer in myList(of Customer) where....
"You could use Case Matches "[a-z]*". It seems more powerful"
I agree that a Matches named keyword would be better then the Like.R but the Matches would be a new reserved keyword what would cause name conflicts with some old source codes.
"...could be transformed to a Regex by the compiler."
If you mean that the present using Like pattern string would be converted automatically to RegEx pattern string and after that the Like operator would use RegEx syntax in the next version VB.
It's an interesting idea but this would work only if the compiler would determine that a pattern string whether an old Like syntax or a RegEx syntax.
I'm not sure that it would be always determined.
For example, when a source code would be pasted from clipboard.
You could use Case Matches "[a-z]*". It seems more powerful
You could also provide both where IS LIKE or better LIKE could be transformed to a Regex by the compiler. The LIKE operator is much simpler to use and should work like the LINQ LIKE
More importantly, the Select Case could use a ISTYPE or TYPEOF comparison that would match on type (including parent classes)
"That said is the Like operator still useful to you compared to say RegEx – which is more powerful?"
I think, yes.
The Like operator is absolutely useful.
It has a minimal pattern syntax for the most frequently used pattern matching.
It may usable very well with the base string operations:
advantages of the Like operator:
- it's an operator not a function (IsMatch)
- built-in, no need to use namespace
- easy to remember the syntax
- easy to learn
- easy to use
- easy to read
- rapid development
Nevertheless, I am not against the RegEx but I use it rarely because of the above.
Even, I am sure that I would use more frequently the RegEx syntax instead of the Like syntax if the RegEx would have a built-in operator.
Otherwise I have a syntax idea for a built-in RegEx operator without a new reserved keyword and without braking change.
I can imagine three kind of syntax for the best known regular expressions:
The pattern syntax of the Like.Q operator would be equivalent with the pattern syntax of the SQL Like operator.
The pattern syntax of the Like.R operator would be equivalent with the pattern syntax of the RegEx.