我得到這樣的字串:
[(0 -0,01)*(0,724-0) (-0,01 -17,982)*(0,725-0,724) (-17,982 -17,983)*(1,324-0,725) (-17,983 -30,587)*(1,323-1,324) (-30,587 -30,587)*(-0,004-1,323) (-30,587 0)*(0--0,004)]*0,5
我必須以 --xx,xx 或 -xx,xx 將設定在括號 -(-xx,xx) 或 (-xx,xx) 中的方式進行轉換
像這樣:
[(0 (-0,01))*(0,724-0) (-0,01 (-17,982))*(0,725-0,724) (-17,982 (-17,983))*(1,324-0,725) (-17,983 (-30,587))*(1,323-1,324) (-30,587 (-30,587))*(-0,004-1,323) (-30,587 0)*(0-(-0,004))]*0,5
但我不知道這樣做。
uj5u.com熱心網友回復:
您可以嘗試正則運算式,例如
using System.Text.RegularExpressions;
...
string source =
"[(0 -0,01)*(0,724-0) (-0,01 -17,982)*(0,725-0,724) (-17,982 -17,983)*(1,324-0,725) (-17,983 -30,587)*(1,323-1,324) (-30,587 -30,587)*(-0,004-1,323) (-30,587 0)*(0--0,004)]*0,5";
string result = Regex.Replace(source, @"(?<=[ -])-[0-9] (\,[0-9] )?", "($&)");
Console.Write(result);
結果:
[(0 (-0,01))*(0,724-0) (-0,01 (-17,982))*(0,725-0,724) (-17,982 (-17,983))*(1,324-0,725) (-17,983 (-30,587))*(1,323-1,324) (-30,587 (-30,587))*(-0,004-1,323) (-30,587 0)*(0-(-0,004))]*0,5
在這里我們尋找(?<=[ -])-[0-9] (\,[0-9] )?模式:
(?<=[ -]) - look ahead for or -
- - "-"
[0-9] - one or more digit in 0..9 range - integer part
(\,[0-9] )? - optional fractional part: comma then one or more digit in 0..9 range
我們包裝的每場比賽(..)-"($&)"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331414.html
上一篇:NETCore用戶身份分組
