0

可能重复:
正则表达式匹配太多

测试字符串:

[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:" + AZ 或 az + "]"
  • group3 => "[IP:" + (.*) + "]"

我已经尝试了许多正则表达式的变体,可以让整个测试字符串返回或什么都不返回......我可以弄清楚如何实际分割它并只返回子字符串。

尝试过的模式包括但不限于:

  • @"^.*$"
  • @"^[.*].*$"
  • @"^(\[.*\])(.*)$"
  • @"^(\[.*\])(\[.*\])(\[.*\])$"
  • @"^(\[TA{[10],}\])(\[.*\])(\[.*\])$" ... ETC。

调用代码:

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 { }
}
4

1 回答 1

0

测试

[TA:1010100][FN:AmplifySignal][IP:(Factor|System.Double|"1.5")(Source|System.String|"Sourcetest")(Destination|System.String|"DestTest")]

图案

string pattern = @"^(\[.*?\])?(\[.*?\])?(\[.*?\])?$";

输出:

G0 - 
    [TA:1010100][FN:AmplifySignal][IP:(Factor|System.Double|"1.5")(Source|System.String|"Sourcetest")(Destination|System.String|"DestTest")]
G1 - 
    [TA:1010100]
G2 - 
    [FN:AmplifySignal]
G3 - 
    [IP:(Factor|System.Double|"1.5")(Source|System.String|"Sourcetest")(Destination|System.String|"DestTest")]
于 2013-01-15T20:44:12.523 回答