4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
r_honey
  • 883
  • 4
  • 15
  • 31
  • 1
    What do these regex do? Not everyone learned Perl but they may know the .net Regex. – kennytm May 19 '10 at 07:19
  • Well, that's what I am trying to find out. If some Perl guy can explain in simple English, what these regex do, I would be able to convert them to .NET!!! – r_honey May 19 '10 at 07:31
  • I think that's the problem for the OP as well :) – pdbartlett May 19 '10 at 07:32
  • Your first two expressions carry an additional comma – I’m no Perl person but I don’t think they belong there … and *if* they do then these expressions might do something else than just replace the strings in the expressions. – Konrad Rudolph May 19 '10 at 08:22
  • Possible duplicate: *[Translate Perl regular expressions to .NET](https://stackoverflow.com/questions/3417644/translate-perl-regular-expressions-to-net)* – Peter Mortensen Jun 16 '20 at 22:27

2 Answers2

3

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
    ...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Quinlan
  • 2,639
  • 1
  • 20
  • 23
  • Hi Daniel, so you mean, the first part is a substitution pattern (s/text //) while the second part is a normal regex search?? – r_honey May 19 '10 at 07:54
  • Thanks Daniel, can you please also comment on this: if($letter =~ m/$first_char/i) – r_honey May 19 '10 at 08:15
  • Any particular reason for using the bloated `String.Empty` in place of just `""`? – Konrad Rudolph May 19 '10 at 08:17
  • But you know, I think Regex itself is a little bloated for this job in VB.net. Built-in string operations should cover these operations just fine. – Daniel Quinlan May 19 '10 at 08:29
  • 1
    "" and string.Empty were slightly different beasts, at least back in .NET 1.0 and 1.1 (see http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx) but I've no idea if this has changed since. I got into the habit of using string.Empty back then and just haven't kicked it yet! – Daniel Renshaw May 19 '10 at 08:30
  • @Daniel Renshaw: They actually haven’t – so yes, **technically** there is a slight difference but I’ll challenge anyone who claims that this is a valid reason to use the bogus construct `String.Empty`. If you do, at least be consistent and use `Int32.Zero` instead of `0`. Oh wait, that doesn’t exist …. – Konrad Rudolph May 19 '10 at 08:39
  • 1
    @daniel `m` here does not mean «multiply lines». Here `m/.../` means «whatever is enclosed with `/` is a regex pattern». And `m/$first_char/i` searches for *pattern* represented by variable `$first_char`, not for *string*. There will be no difference if `$first_char` does not contain special symbols, but without other code you cannot be sure. – ZyX May 19 '10 at 08:47
  • @Konrad: no such claim from me - just offering a bit of information that some may find interesting. Even so, MS presumably felt there was enough of a difference to offer `string.Empty` so maybe "bogus" is putting it a little strongly. However, more reasons to use `""` instead here: http://dotnetperls.com/string-empty and a SO discussion here: http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and – Daniel Renshaw May 19 '10 at 08:51
  • @ZyX, thanks for the correction, I'm deleting my comment on this to avoid confusion. – Daniel Quinlan May 19 '10 at 08:59
0
    $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 :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cine
  • 4,255
  • 26
  • 46