[英]Find all occurrences of substring in string
我有一個包含以下內容的文本文件:
_vehicle_12 = objNull;
if (true) then
{
_this = createVehicle ["Land_Mil_Guardhouse", [13741.654, 2926.7075, 3.8146973e-006], [], 0, "CAN_COLLIDE"];
_vehicle_12 = _this;
_this setDir -92.635818;
_this setPos [13741.654, 2926.7075, 3.8146973e-006];
};
我想查找{
和};
之間的所有匹配項};
並分配以下字符串:
string direction = "_this setDir" value, in example _vehicle_12 it would mean that:
string direction = "-92.635818";
string position = "_this setPos" value, in example _vehicle_12 it would be:
string position = "[13741.654, 2926.7075, 3.8146973e-006]";
我多次出現這些類型,並且每次使用{ };
時都想找出最好的方法{ };
發生以設置方向和位置,然后移動到下一個發生處。
以下代碼可以讀取字符串(將文件保存在大字符串中),並且發現第一個匹配項很好,但是我想對其進行調整,以找到{和}的每個匹配項;
string alltext = File.ReadAllText(@file);
string re1 = ".*?"; // Non-greedy match on filler
string re2 = "(\\{.*?\\})"; // Curly Braces 1
Regex r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(alltext);
if (m.Success)
{
String cbraces1 = m.Groups[1].ToString();
MessageBox.Show("Found vehicle: " + cbraces1.ToString() + "\n");
}
為了幫助您入門:
\{\n([0-z\[\]" ,-\.=]+;\n)+\}
應該返回花括號內的各行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.