.NET equivalent to Perl regular expressions

I need to convert a Perl script to VB.NET. I managed almost all of the conversion, but some Perl (seemingly simple) regular expression causes a headache. Can someone suggest me the .NET equivalent of the following perl regular expression:

1)

$letter =~ s/Users //,; $letter =~ s/Mailboxes //,; if($letter =~ m/$first_char/i){ 

2)

 unless($storegroup =~ /Recovery/ || $storegroup =~ /Users UVWXYZ/ || $storegroup =~ /Users ST/ || $storegroup =~ /Users QR/){ 

Regular expression looks simple to me. I tried to wade through perl.org, but sometimes understands the langugae regular expression, and I need to quickly complete the conversion.

+4
source share
2 answers

In Perl, you can think of slashes as something like double quotes with the added value โ€œbetween these slashes is a regular stringโ€. The first block of code is the Perl find / replace regex:

 $stringvar =~ s/findregex/replaceregex/; 

Takes findregex and replaces it with replaceregex , in place. This example is a very simple search, and the .net Regex class will be redundant. String.Replace() will do the job:

 letter = letter.Replace("Users ", "") letter = letter.Replace("Mailboxes ", "") 

The second part is Perl for search only, returns true if the found findregex string is found, and the actual string itself is not affected.

 $stringvar =~ /findregex/; 

String.Contains() can handle this in .net:

 if (!(storegroup.Contains("Recovery") _ or storegroup.Contains("Users UVWXYZ") _ or storegroup.Contains("you get the idea"))) Then ... 

(sorry if my VB is a little rusty, but hope this helps)

+3
source
 $letter =~ s/Users //,; $letter =~ s/Mailboxes //,; if($letter =~ m/$first_char/i){ 

->

 letter = letter.Replace("Users ", ""); letter = letter.Replace("Mailboxes ", ""); //next one depends on what $first_char is 

and

 unless($storegroup =~ /Recovery/ || $storegroup =~ /Users UVWXYZ/ || $storegroup =~ /Users ST/ || $storegroup =~ /Users QR/){ 

->

 if (!(storegroup.Contains("Recovery") || storegroup.Contains("Users UVWXYZ") ...and so on...)) 

Only reason to use regex here is that Perl is very good at regex :)

0
source

Source: https://habr.com/ru/post/1310202/


All Articles