2012-11-26 55 views
0
String testString = "Some text F4LE8AWMF87E and again some text"; 
Match myMatch = Regex.Match(testString, "\b(?=[a-zA-Z]*[0-9])(?=[0-9]*[a-zA-Z])[0-9a-zA-Z]{12,}\b"); 
myLabel.Text = testString.Substring(myMatch.Index, myMatch.Length); 

myLabel现在应该显示“F4LE8AWMF87E”,但它没有12个字符#得到字符串。正则表达式的C包含至少一个数字,字母

出了什么问题?

+4

F4LE8AWMF87只有11个字符。 – dwo

+0

好吧,我的坏。我只是改变了它。它仍然不起作用。 – SoBiT

+0

我认为用老的循环来实现要容易得多。只需通过字边界'\ b'分割字符串,然后迭代计算字母和数字出现次数的片段。 – J0HN

回答

1

您必须在字符串文字中转义字符'\'。

String testString = "Some text F4LE8AWMF87E and again some text"; 
Match myMatch = Regex.Match(testString, @"\b(?=[a-zA-Z]*[0-9])(?=[0-9]*[a-zA-Z])[0-9a-zA-Z]{12,}\b"); 
myLabel.Text = myMatch.Value; 
+0

给出空字符串,所以它不起作用。你有没有尝试启动它? – J0HN

+0

@ J0HN编辑,再次检查。 – deerchao

+0

现在,它的工作,谢谢。 – SoBiT

0

请尝试以下请:在Rublar

参考尽管C#逃逸,使细微的差别。你可以测试它。我相信你,如果你有在正则表达式的情况下,这将使它更好:)

String testString = "Some text F4LE8AWMF87 and again some text"; 
    Match myMatch = 
    Regex.Match(testString, "\b[a-zA-Z\d]{11,12}\b"); 
    myLabel.Text = testString.Substring(myMatch.Index, myMatch.Length); 
+0

已经有了答案,你只需要在C#中用@:D来转义斜线。 – bonCodigo

相关问题