可能重复:
Regex matching too much
测试字符串:
[TA:1010100][FN:AmplifySignal][IP:(Factor|System.Double|"1.5")(Source|System.String|"Sourcetest")(Destination|System.String|"DestTest")]
此字符串的一般格式分为3组:
我已经尝试了许多正则表达式的变体,可以让整个测试字符串返回或没有...我可以弄清楚如何实际分割它并返回子串。
尝试过的模式包括但不限于:
@"^.*$"
@"^[.*].*$"
@"^(\[.*\])(.*)$"
@"^(\[.*\])(\[.*\])(\[.*\])$"
@"^(\[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 { }
}
答案 0 :(得分: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")]