i get strings like this:
[(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
i have to convert it in a way that --xx,xx or -xx,xx will be set in brackets -(-xx,xx) or (-xx,xx)
like this:
[(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
but i have no idea to do this.
CodePudding user response:
You can try regular expressions, e.g.
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);
Outcome:
[(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
Here we look for (?<=[ -])-[0-9] (\,[0-9] )?
pattern:
(?<=[ -]) - 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
Each match we wrap into (..)
- "($&)"