3

I'm using new Regex("(?m)^\S+ [A-Z]{1,3}$").IsMatch(sStringToCheck) to check a multiline string.

My issue is, that it only appears to validate the last line of the string.

This list passes:

  • ABC-12345-DEF A
  • ABC-12345-DEF A 123
  • ABC-12345-DEF A

This list fails:

  • ABC-12345-DEF A
  • ABC-12345-DEF A
  • ABC-12345-DEF A 123

However, I would like fail both, as each contains a not matching line.

Thanks!

4

2 回答 2

5
bool match = sStringToCheck
            .Split(new string[] {Environment.NewLine}, StringSplitOptions.None)
            .Any(line => new Regex(@"^\S+ [A-Z]{1,3}$").IsMatch(line));

将检查每一行。那是你的目标吗?

于 2013-08-12T15:09:49.107 回答
1

Jonesy 的回答是一种更好的方法,但是为了彻底,一种非 LINQ-y 的方法是遍历每一行,或者得到你的整体答案:

bool match = false;
foreach(string s in sStringToCheck.Split(new string[]{Environment.NewLine}, StringSplitOptions.None)){
    match = match | new Regex("(?m)^\S+ [A-Z]{1,3}$").IsMatch(s);
}
于 2013-08-12T15:23:19.420 回答