2011-05-02 75 views
1

我在这里有点难住。我有这个方法,这是工作的罚款,直到最近才:C#Regex;双引号问题

internal static bool IsZplFormat(string szString) 
{ 
    var regex = new Regex(@"\^XA.*\^XZ\\r\\n"); 
    return regex.IsMatch(szString); 
} 

,将工作细给出下面的字符串(从我的单元测试所):

const string zplSample = "^XA blah blah blah ^XZ\r\n"; 

什么似乎现在要发生的事情,是我得到这样的事情:

const string zplSample = "^XA blah blah \"blah ^XZ\r\n"; 

现在我的正则表达式不再匹配了。

我认为.*应该匹配所有字符,但它似乎越来越绊倒在这个双引号。关于如何让这项工作再次发生的任何想法?

+0

所以是输入失败'@“^ XA等等等等!”“等等^ XZ \ r \ n”'? – 2011-05-02 18:37:44

+0

我认为这是我混乱的一部分。我发布的字符串是我回到调试器时获得的内容,并且“Copy Value” – 2011-05-02 18:38:55

回答

1

测试这里...

string zplSample = "^XA blah blah blah ^XZ\r\n"; 
string zplSample1 = "^XA blah blah \"blah ^XZ\r\n"; 

Console.WriteLine(new Regex(@"\^XA.*\^XZ\r\n").IsMatch(zplSample)); 
Console.WriteLine(new Regex(@"\^XA.*\^XZ\r\n").IsMatch(zplSample1)); 

Console.ReadKey(); 

输出

True 
True 

我是怎么改变?正则表达式为@"\^XA.*\^XZ\r\n"。 (从两个backslashes到一个)(\\r\\n

+0

OK;这可能就是我在漫长的一天之后失去它,但我没有看到两个正则表达式之间有任何区别 – 2011-05-02 18:49:20

+0

行;我正在失去它。无视之前的评论 – 2011-05-02 18:50:06

+0

哈哈,那发生我知道:P – BrunoLM 2011-05-02 18:52:05