tags:

views:

109

answers:

2

I need to convert a Perl script to VB.NET. I have managed almost the entire conversion, but some Perl (seemingly simple) regex are causing an headache. Can someone suggest me .NET equivalent of the following perl regex:

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 regex look simple to me. I tried to wade through perl.org but understanding a langugae regex takes sometime and I need to complete the conversion quickly.

+3  A: 

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 regex:

$stringvar =~ s/findregex/replaceregex/;

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, returns true if the findregex string is found, 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 
    ...

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

daniel
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
yes exactly, just edited my answer to reflect that :)
daniel
Thanks Daniel, can you please also comment on this:if($letter =~ m/$first_char/i)
r_honey
Any particular reason for using the bloated `String.Empty` in place of just `""`?
Konrad Rudolph
no, no. not really.
daniel
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
"" 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
@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
@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
@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
@ZyX, thanks for the correction, I'm deleting my comment on this to avoid confusion.
daniel
A: 
$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 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...))

Only reason to use regex here is because Perl is super good at regex :)

Cine