【发布时间】:2017-05-10 10:34:43
【问题描述】:
假设A 1、A 12、A 123、A 1234 和B 1、B 12、B 123、B 1234 相同,其中 123 表示三位数字
现在到我这样做的时候:
MatchCollection ForA1 = Regex.Matches(inFile, @"\b(A [0-9])\b");
MatchCollection ForA2 = Regex.Matches(inFile, @"\b(A [0-9][0-9])\b");
.... and so on for three and four digits and B; total 8 lines
为了减少我这样做的代码:
MatchCollection ForAB1 = Regex.Matches(inFile, @"\b(A [0-9]|B [0-9])\b");
MatchCollection ForAB2 = Regex.Matches(inFile, @"\b(A [0-9][0-9]|B [0-9][0-9])\b");
.... and so on for three and four digits; total 4 lines
现在我想这样做:
MatchCollection ForAB1234 = Regex.Matches(inFile, @"\b(A [0-9]|B [0-9]...
|A [0-9][0-9]|B [0-9][0-9] and so on for three and four digits )\b"); total 1 line
比赛结束后我会这样做:
foreach (Match m in ForAB1)
{
//many calculations on the basis of index and length etc
}
我想要什么:
foreach (Match m in ForAB1)
{
if(Match is 'A [0-9]')
{//many calculations on the basis of index and length etc}
else...
}
还有什么足够简单的,以至于我不需要仅仅因为不同的位数而重复代码吗?我正在寻找我通过管道传输的所有不同的匹配项。
编辑:真正的问题是我不想 m.len 然后检查它是 A 还是 B,因为实际上我有 30 多个这样的表达式
【问题讨论】:
-
无意冒犯,但如果你有 -1 这个问题应该有适当的理由,我认为你可以在这里写