【发布时间】:2013-01-15 20:34:39
【问题描述】:
测试字符串:
[TA:1010100][FN:AmplifySignal][IP:(Factor|System.Double|"1.5")(Source|System.String|"Sourcetest")(Destination|System.String|"DestTest")]
此字符串的一般格式分为 3 组:
- group1 => "[TA:" + 1 或 0 重复 + "]"
- group2 => "[FN:" + A-Z 或 a-z + "]"
- group3 => "[IP:" + (.*) + "]"
我已经尝试了许多正则表达式的变体,可以让整个测试字符串返回或什么都不返回...我可以弄清楚如何实际分割它并只返回子字符串。
尝试过的模式包括但不限于:
@"^.*$"@"^[.*].*$"@"^(\[.*\])(.*)$"@"^(\[.*\])(\[.*\])(\[.*\])$"-
@"^(\[TA{[10],}\])(\[.*\])(\[.*\])$"...等等。
调用代码:
BindingList<Tuple<bool[], IFactory>> Recipe = new BindingList<Tuple<bool[], IFactory>>();
var amp = new Factory.AmplifySignal();
amp.Destination = "DestTest";
amp.Source = "Sourcetest";
amp.Factor = 1.5;
bool[] Ts = new bool[] { true, false, true, false, true, false, false };
var CI = new CompactInstruction(Ts, amp.GetFactoryKey(), amp.GetProperties());
string TestString = CI.ToString();
Console.WriteLine(TestString);
string pattern = @"^(\[.*\])?"; //Have been adjusting in debug mode
while (true)
{
try
{
Match Result = Regex.Match(TestString, pattern, RegexOptions.IgnoreCase);
if (Result.Success)
{
for (int i = 0; i < Result.Groups.Count; i++)
{
Console.WriteLine("G{0} - \r\n\t{1}", i, Result.Groups[i]);
}
}
}
catch { }
}
【问题讨论】:
-
为什么不能应用3个正则表达式?一个综合的、超级复杂的能给你带来什么?
-
啊废话,当然我关注的下一个链接回答了我的问题......我需要懒惰搜索stackoverflow.com/questions/1373966/regex-matching-too-much