相当于Perl的正则EX pressions .NET [英] .NET equivalent to Perl regular expressions

查看:201
本文介绍了相当于Perl的正则EX pressions .NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个Perl脚本转换为VB.NET。我已成功几乎整个转换,但一些Perl的(貌似简单)的正则表达式是引起头痛。有人建议我.NET相当于下面的Perl的正则表达式的:

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/){

正则表达式看起来简单给我。我试图涉水通过perl.org但了解一个langugae正则表达式需要一段时间,我需要尽快完成转换。

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.

推荐答案

在Perl中,你能想到的斜线为类似双引号的这些斜线之间是一个正则表达式串的附加意义。的code中的第一个块是一个Perl查找/替换正则表达式:

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/;

注意到 findregex ​​ replaceregex ​​替换它,就地。给出的例子是一个非常简单的搜索,和.NET正则表达式类是杀鸡用牛刀。 与string.replace()方法将做的工作:

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 ", "")

第二部分是对的Perl只找到,返回如果发现findregex串,离开实际字符串本身不变。

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()可以处理这个在.NET:

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

(抱歉,如果我VB是有点生疏,但希望这有助于)

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

这篇关于相当于Perl的正则EX pressions .NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆