Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 and C# regexes

I need to convert a VB6(which I'm not fammiliar with) project to C# 4.0 one. The project contains some regexes for string validation.

I need to know if the regexes behave the same in both cases, so if i just copy the regex string from the VB6 project, to the C# project, will they work the same?

I have a basic knowledge of regexes and I can just about read what one does, but for flavors and such, that's a bit over my head at the moment.

For example, are these 2 lines equivalent?

VB6:

isStringValid = (str Like "*[!0-9A-Z]*")

C#:

isStringValid = Regex.IsMatch(str, "*[!0-9A-Z]*");

Thanks!

like image 997
Ioana O Avatar asked Feb 18 '23 21:02

Ioana O


1 Answers

The old VB Like operator, despite appearances, is not a regular expression interface. It's more of a glob pattern matcher. See http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx

In your example:

Like "*[!0-9A-Z]*"

Matches strings that start and end with any character (zero or more), then doesn't match an alphanumeric character somewhere in the middle. The regular expression for this would be:

/.*[^0-9A-Z].*/

EDIT To answer your question: No, the two can't be used interchangeably. However, it's fairly easy to convert Like's operand into a proper regular expression:

Like       RegEx
========== ==========
?          .
*          .*
#          \d
[abc0-9]   [abc0-9]
[!abc0-9]  [^abc0-9]

There are a few caveats to this, but that should get you started and cover most cases.

like image 198
Xophmeister Avatar answered Feb 27 '23 19:02

Xophmeister