Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Like operator confusion with less-than sign in pattern

Dim rc As Boolean = "2" Like "*?<*?"

I don't understand why rc equals True, surely 2 is not like *?<*? at all.

The above pattern requires a string with

  • At least three characters (two ?'s and a <)
  • where the < symbol is somewhere on the interior.

As far as I can work out < is not a special character that means something other than < to the Like operator.

Using Visual Studio 2010.

like image 302
David Rutten Avatar asked Nov 12 '22 11:11

David Rutten


1 Answers

While I can't directly explain why 2 is like "*?<*?".

Your query reads;

  • * - Match 0 or more characters
  • ? - Followed by one single character
  • < - Followed by the < character
  • * - Followed by 0 or more characters
  • ? - Terminated by one single character

For your logic you want;

at least three characters before it matches, with "<" occurring somewhere on the interior of the string

Which results in a query of;

Dim rc As Boolean = "2" Like "???*<*"

Which reads;

  • ??? - Match at least 3 characters
  • * - Followed by any number of further characters
  • < - Followed by the < character
  • * - Followed by any number of further characters

Not a direct answer I know, but I hope it helps all the same...

EDIT:

To answer your comment below.

You would like to;

find a string which has a "<" in it somewhere with at least one character on either side

Which results in a query of;

Dim rc As Boolean = "2" Like "*?<?*"

This would return False as a result, as would;

Dim rc As Boolean = "<" Like "*?<?*"

However;

Dim rc As Boolean = "2<2" Like "*?<?*"

Would return true.

I hope this helps (more!)

like image 188
PGallagher Avatar answered Dec 05 '22 14:12

PGallagher