.NET equivalent to Perl regular expressions
.net, perl, regex
Solution
In Perl, you can think of the slashes as something like double-quotes with the added meaning of "between these slashes is a regex-string". The first block of code is a Perl find/replace regular expression:
$stringvar =~ s/findregex/replaceregex/;
It takes `findregex` and replaces it with `replaceregex`, in-place. The given example is a very simple search, and the .NET Regex class would be overkill. `String.Replace()` method will do the job:
letter = letter.Replace("Users ", "")
letter = letter.Replace("Mailboxes ", "")
The second part is Perl for find only. It returns `true` if the findregex string is found and leaves the actual string itself untouched.
$stringvar =~ /findregex/;
`String.Contains()` can handle this in .NET:
if (!(storegroup.Contains("Recovery") _
or storegroup.Contains("Users U V W X Y Z") _
or storegroup.Contains("you get the idea"))) Then
...
Problem
I need to convert a Perl script to VB.NET. I have managed almost the entire conversion, but some Perl (seemingly simple) regular expressions are causing an headache. What is the .NET equivalent of the following Perl regular expressions? 1) ``` $letter =~ s/Users //,; $letter =~ s/Mailboxes //,; if($letter =~ m/$first_char/i){ ``` 2) ``` unless($storegroup =~ /Recovery/ || $storegroup =~ /Users U V W X Y Z/ || $storegroup =~ /Users S T/ || $storegroup =~ /Users Q R/){ ``` The regular expressions look simple to me. I tried to wade through perl.org, but understanding a language's regular expressions takes some time.