开发者

.NET equivalent to Perl regular expressions

开发者 https://www.devze.com 2022-12-30 15:11 出处:网络
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

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 Per开发者_Python百科l 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.


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
    ...


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

-->

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

and

    unless($storegroup =~ /Recovery/ || $storegroup =~ /Users U V W X Y Z/ || $storegroup =~ /Users S T/
    || $storegroup =~ /Users Q R/){

-->

    if (!(storegroup.Contains("Recovery") || storegroup.Contains("Users U V W X Y Z") ...and so on...))

The only reason to use regular expressions here is because Perl is super good at regular expressions :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号