简体   繁体   English

在VB.Net中使用perl regex构造?

[英]Using perl regex constructs in VB.Net?

In perl you can write 在perl中,您可以编写

$string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/;

for example. 例如。

Is it possible to achieve the same "translation" effects with VB.Net regexes? 使用VB.Net正则表达式是否可以实现相同的“翻译”效果?

Thanks you! 谢谢!

PS: I'm not searching for a way to port this very example, it's more of a curiosity question :) PS:我不是在寻找移植此示例的方法,更多的是好奇心问题:)

There is no standard method for this. 没有为此的标准方法。 You can do it by iterating over each character in your input string and using a dictionary to map it to another character (or leave it unchanged if the character is not found in the dictionary). 您可以通过遍历输入字符串中的每个字符并使用字典将其映射到另一个字符(或者如果在字典中未找到该字符,则将其保留不变)来做到这一点。 The result can be built using a StringBuilder for performance reasons. 由于性能原因,可以使用StringBuilder构建结果。

If performance is not an issue then you might be able to use a few replace operations instead: 如果性能不是问题,那么您也许可以使用一些替换操作:

s = s.Replace("a", "A")
     .Replace("e", "E")
     ...
     .Replace("y", "Y");

Here's one way to do this: 这是执行此操作的一种方法:

public string fakeTR(string theString, char[] org, char[] rep)
{
  for(int i=0;i<org.lenght;i++)
  {
    theString = theString.Replace(org[i], rep[i]);
  }
  return theString;
}

You would be able to call it with somewhat clunky but shorter:

string v = "Black in South Dakota";
v = fakeTR(v, new char[]{'B','l','a','c','k'}, new char[]{'W','h','i','t','e'}); 

H/T http://discuss.joelonsoftware.com/default.asp?dotnet.12.306220.6 H / T http://discuss.joelonsoftware.com/default.asp?dotnet.12.306220.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM