2

If you guys decide to put this code into your complier, you will notice that I get a Implicitly-typed local variables must be initlialized error on the var match. But i'm not too sure about how to go about doing this check (for text.count()) any other way. Sometimes my OCR will miss a letter or something and if it does, the number of characters will be less and that will make the match variable return a null...leaving me without a result.

Also, for bonus points, anyone that can help clean up my REGEX expression that'd be nice..i just started using it recently and i'm not tooo skilled in the semantics yet.

var match;
if (wordList[i].Text.Count() < 12)
{
    match = Regex.Match(wordList[i].Text, @"c[:;i]ty[!l1;:i/][5s][tuj!l/]z[i;:l]p[c1:;zs]", RegexOptions.IgnoreCase);
}
else
{
    match = Regex.Match(wordList[i].Text, @"c[:;i]ty[!l1;:i/][5s][tuj!l/][f!l1j;:i/]z[i;:l]p[c1:;zs]", RegexOptions.IgnoreCase);
}
if (match.Success)
{ ... }
4

5 回答 5

3

你不能在var没有初始化的情况下声明它。利用

Match match;

或者如果你真的必须,

dynamic match;//I don't recommend this

Var是一个隐式类型的变量,你必须给它赋值才能推断出实际的类型。见这里

于 2013-07-24T16:55:38.327 回答
0

在不查看您的正则表达式模式的情况下,我建议您可以通过将“模式决策逻辑”与其余代码分开来分解您的代码。

下面我提供了 2 个单独的示例来说明如何分解。第一个定义了一个DeterminePattern方法,它只返回要用作正则表达式模式的字符串。第二个定义了一个FindMatch返回适当匹配对象的方法。

从技术上讲,其中任何一个都允许您继续使用 var 关键字,但在我看来,这并不是那么重要……在我看来,这里的好处是使代码更易于阅读和理解。您可以自己决定是否像这样分解代码来实现这一点。

示例 1

private static string DeterminePattern(string input)
{
    if (input.Count() < 12)
    {
        return @"...";
    }

    return @"...";
}


var match = Regex.Match(wordList[i].Text, DeterminePattern(wordList[i].Text), RegexOptions.IgnoreCase);
if (match.Success)
{ ... }

示例 2

private static Match FindMatch(string input)
{
    if (input.Count() < 12)
    {
        return Regex.Match(input, @"...", RegexOptions.IgnoreCase);
    }

    return Regex.Match(input, @"...", RegexOptions.IgnoreCase);
}

var match = FindMatch(wordList[i].Text);
if (match.Success)
{ ... }
于 2013-07-24T17:21:49.863 回答
0

请检查文档

从 Visual C# 3.0 开始,在方法范围内声明的变量可以具有隐式类型 var。隐式类型的局部变量是强类型的,就像您自己声明了类型一样,但编译器确定类型。

当你声明一个变量时,你需要使用显式类型的方式,或者做

var match = new Match(); 
于 2013-07-24T17:08:02.563 回答
0

你必须给编译器一些东西,以便它可以推断出类型是什么。例如,这将起作用:

var match = (Match)null;

因为编译器知道var应该是一个Match.

你为什么要重新做这件事Match match;超出了我的理解,但它确实有效。

于 2013-07-24T17:08:58.597 回答
0

保持干燥:不要重复自己。

如果您必须使用var(即使您不使用:var特别是在这种情况下使用的意义何在?),这个结构更好:

string text = wordList[i].Text ;
string pattern = text.Length < 12
               ? @"c[:;i]ty[!l1;:i/][5s][tuj!l/]z[i;:l]p[c1:;zs]"
               : @"c[:;i]ty[!l1;:i/][5s][tuj!l/][f!l1j;:i/]z[i;:l]p[c1:;zs]"
               ;
Match match = Regex.Match( text , pattern , RegexOptions.IgnoreCase ) ;

或者,更好的是:

string text  = wordList[i].Text ;
Regex  rx    = text.Length < 12 ? Regex1 : Regex2 ;
Match  match = rx.Match( text , pattern , RegexOptions.IgnoreCase ) ;
.
.
.
private static readonly Regex1 = new Regex( @"c[:;i]ty[!l1;:i/][5s][tuj!l/]z[i;:l]p[c1:;zs]" , RegexOptions.IgnoreCase ) ;
private static readonly Regex2 = new Regex( @"c[:;i]ty[!l1;:i/][5s][tuj!l/][f!l1j;:i/]z[i;:l]p[c1:;zs]" , RegexOptions.IgnoreCase ) ;

你到底为什么要使用 Linq ( Wordlist[i].Text.Count()) 来获取字符串的长度?字符串已经知道长度。无需遍历它来计算字符数:只需通过查询字符串的Length属性来询问字符串有多长。

于 2013-07-24T17:55:56.057 回答