[英]Regular expression causing my failing unit tests. Why?
I am having a really tough time with regular expressions. 我在正则表达式方面很难过。 Mine is causing my unit test to fail. 我的导致我的单元测试失败。 I have tried it a couple of different ways without success. 我尝试了几种不同的方法,但均未成功。 Maybe I am going about it wrong. 也许我要去做错了。 I need it to either use one word (Trumpet) or two words with a space (French Horn). 我需要它使用一个单词(小号)或两个单词之间加一个空格(法国号)。
Property that I am having an issue with. 我遇到问题的财产。
public string Name
{
get { return _name; }
set
{
string source = propTI.ToTitleCase(value.Trim());
string pattern = "^[A-Z][a-z]*\\s[A-Z][a-z]*$";
if (Regex.IsMatch(source, pattern))
_name = source;
else
{
throw new ArgumentException("Name must have proper case!");
}
}
}
I have also tried "^([AZ][az] \\s) $" as a pattern. 我也尝试过使用“ ^([AZ] [az] \\ s) $”作为模式。
Constructor: 构造函数:
public Instrument() : this(DefaultName, DefaultCategory) { }
Unit Test: 单元测试:
[TestMethod]
public void Instrument_Name_IsValid()
{
var na = "french";
var goodna = "French";
var inst = new Instrument();
inst.Name = na;
Assert.AreEqual(goodna, inst.Name);
}
What should I use for a regular expression? 正则表达式应该使用什么? Since these are clearly not working? 既然这些显然行不通?
Casimir et Hippolyte有答案:
^[A-Z][a-z]*(?:\\s[A-Z][a-z]*)*$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.