I would like to take a string representation of a set and parse it. Does anybody know of any pre-existing libraries?
I can write the regular expression to match these patterns, but it doesn't seem to me to be a very efficient method of doing things. Also some edge cases will allow certain strings to pass which shouldn't, and I'm probably missing some other cases.
For example, I would like to parse strings like the following:
"{10,-20,30.5,-40.01,.5,-.5}"
//^\{((-?[0-9]*(\.[0-9]+)?)(?:,|\}))+$
.Parse()
.ShouldEqual(new [] { 10,-20,30.5,-40.01,.5,-.5 });
"{(10,10),(20,2),(30.2,-5.5),(-5,40),(-.2,3000),(-.1,-.2)}"
//^\{(\((-?[0-9]*(\.[0-9]+)?),(-?[0-9]*(\.[0-9]+)?)\)(?:,|}))+$
.Parse()
.ShouldEqual(new [] {
new Point(10,10),
new Point(20,2),
new Point(30.2,-5.5),
new Point(-5,40),
new Point(-.2,3000),
new Point(-.1,-.2)
});
"{(10,[10,1]),(20,[2,4]),(30.2,[-5.5,-10]),(-5,[40,10]),(-.2,[3000,10]),(-.1,[-.2,0])}"
//^\{(\((-?[0-9]*(\.[0-9]+)?),\[((-?[0-9]*(\.[0-9]+)?)(?:,|\]))+\)(?:,|}))+$
.Parse()
.ShouldEqual(new [] {
new Point(10,new[]{ 10,1 }),
new Point(20,new[]{2,4}),
new Point(30.2,new[]{-5.5,-10}),
new Point(-5,new[]{40,10}),
new Point(-.2,new[]{3000,10}),
new Point(-.1,new[]{-.2,0})
});
The Point class is arbitrary.