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],}\])(\[.*\])(\[.*\])$" ...など

呼び出しコード:

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 に答える