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
Thanks for the suggestion. We’re moving language design to GitHub. Please feel free to refile at https://github.com/dotnet/vblang if this suggestion is still something you advocate.
You can do this:
Select Case True
Nooooo. Do not use regex. It takes a PhD to understand regex and a lifetime career to write regex.
@Visual Studio Team: The Like operator covers 99% of all use cases of advanced string comparison, offering a much cleaner and simpler syntax than RegEx.
I only needed RegEx once or twice in my 15 years career. I even use Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString when writing C#.
I would support an "extension" type of syntax, that would work somewhat like the With pattern does.
Select Case someString
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.