Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#从字符串捕获点类型_C#_Regex_Point_Trim - Fatal编程技术网

c#从字符串捕获点类型

c#从字符串捕获点类型,c#,regex,point,trim,C#,Regex,Point,Trim,我通过NetworkStream发送一个字符串,其中点的格式如下: (x,y)(x,y)(x,y)(x,y) 我想在System.Drawing.Point数组中重新转换此字符串。 我该怎么办 感谢您的帮助您可以这样使用Regex string S = "(1,2)(33,44)(55,66)(77,8888)"; Regex R = new Regex(@"\((\d|\,)+\)"); foreach (Match item in R.Matches(S)) { var P = i

我通过
NetworkStream
发送一个字符串,其中点的格式如下:

(x,y)(x,y)(x,y)(x,y)
我想在
System.Drawing.Point
数组中重新转换此字符串。 我该怎么办


感谢您的帮助

您可以这样使用
Regex

string S = "(1,2)(33,44)(55,66)(77,8888)";
Regex R = new Regex(@"\((\d|\,)+\)");
foreach (Match item in R.Matches(S))
{
    var P = item.Value.Substring(1,item.Value.Length-2).Split(',');
    Point YourPoint = new Point(int.Parse(P[0]), int.Parse(P[1]));
    MessageBox.Show(YourPoint.ToString());
}

您可以这样使用
Regex

string S = "(1,2)(33,44)(55,66)(77,8888)";
Regex R = new Regex(@"\((\d|\,)+\)");
foreach (Match item in R.Matches(S))
{
    var P = item.Value.Substring(1,item.Value.Length-2).Split(',');
    Point YourPoint = new Point(int.Parse(P[0]), int.Parse(P[1]));
    MessageBox.Show(YourPoint.ToString());
}

您可以使用正则表达式,然后解析字符串,全部使用LINQ

string args = "(4,1)(7,5)(5,4)(2,3)"; // Test data

return Regex.Matches(args, @"\(([^)]*)\)")
            .Cast<Match>()
            .Select(c => 
                   {
                       var ret = c.Groups[1].Value.Split(',');
                       return new Point(int.Parse(ret[0]), int.Parse(ret[1]));
                   }))
string args=“(4,1)(7,5)(5,4)(2,3)”;//测试数据
返回Regex.Matches(args,@“\([^)]*)\)”)
.Cast()
.选择(c=>
{
var ret=c.Groups[1]。Value.Split(',');
返回新点(int.Parse(ret[0]),int.Parse(ret[1]);
}))

您可以使用正则表达式,然后解析字符串,全部使用LINQ

string args = "(4,1)(7,5)(5,4)(2,3)"; // Test data

return Regex.Matches(args, @"\(([^)]*)\)")
            .Cast<Match>()
            .Select(c => 
                   {
                       var ret = c.Groups[1].Value.Split(',');
                       return new Point(int.Parse(ret[0]), int.Parse(ret[1]));
                   }))
string args=“(4,1)(7,5)(5,4)(2,3)”;//测试数据
返回Regex.Matches(args,@“\([^)]*)\)”)
.Cast()
.选择(c=>
{
var ret=c.Groups[1]。Value.Split(',');
返回新点(int.Parse(ret[0]),int.Parse(ret[1]);
}))

我试图解析它:

        string s = "(1,2)(2,3)(3,4)(4,5)";

        var rawPieces = s.Split(')');

        var container =  new List<System.Drawing.Point>();
        foreach(var coordinate in rawPieces)
        {
            var workingCopy = coordinate.Replace("(",String.Empty).Replace(")",String.Empty);
            if(workingCopy.Contains(","))
            {
                var splitCoordinate = workingCopy.Split(',');
                if(splitCoordinate.Length == 2)
                {
                    container.Add(new System.Drawing.Point(Convert.ToInt32(splitCoordinate[0]),Convert.ToInt32(splitCoordinate[1])));
                }
            }
        }

        Console.WriteLine(container.Count);
string s=“(1,2)(2,3)(3,4)(4,5)”;
var rawPieces=s.Split(');
var container=新列表();
foreach(原始件中的var坐标)
{
var workingCopy=coordinate.Replace(“(”,String.Empty)。Replace(“)”,String.Empty);
if(workingCopy.Contains(“,”))
{
var splitCoordinate=workingCopy.Split(',');
if(splitCoordinate.Length==2)
{
container.Add(新的System.Drawing.Point(Convert.ToInt32(拆分坐标[0]),Convert.ToInt32(拆分坐标[1]));
}
}
}
控制台写入线(容器计数);

我试图解析它:

        string s = "(1,2)(2,3)(3,4)(4,5)";

        var rawPieces = s.Split(')');

        var container =  new List<System.Drawing.Point>();
        foreach(var coordinate in rawPieces)
        {
            var workingCopy = coordinate.Replace("(",String.Empty).Replace(")",String.Empty);
            if(workingCopy.Contains(","))
            {
                var splitCoordinate = workingCopy.Split(',');
                if(splitCoordinate.Length == 2)
                {
                    container.Add(new System.Drawing.Point(Convert.ToInt32(splitCoordinate[0]),Convert.ToInt32(splitCoordinate[1])));
                }
            }
        }

        Console.WriteLine(container.Count);
string s=“(1,2)(2,3)(3,4)(4,5)”;
var rawPieces=s.Split(');
var container=新列表();
foreach(原始件中的var坐标)
{
var workingCopy=coordinate.Replace(“(”,String.Empty)。Replace(“)”,String.Empty);
if(workingCopy.Contains(“,”))
{
var splitCoordinate=workingCopy.Split(',');
if(splitCoordinate.Length==2)
{
container.Add(新的System.Drawing.Point(Convert.ToInt32(拆分坐标[0]),Convert.ToInt32(拆分坐标[1]));
}
}
}
控制台写入线(容器计数);

您可以尝试使用
Regex
解析它:

string str = @"(11,22)(2,3)(4,-10)(5,0)";
Regex r = new Regex(@"(-?[0-9]+),(-?[0-9]+)");
Match m = r.Match(str);
var points = new List<System.Drawing.Point>();
while (m.Success)
{
    int x, y;
    if (Int32.TryParse(m.Groups[1].Value, out x) && Int32.TryParse(m.Groups[2].Value, out y))
    {
        points.Add(new System.Drawing.Point(x, y));
    }
    m = m.NextMatch();
}
字符串str=@“(11,22)(2,3)(4,-10)(5,0)”; 正则表达式r=新正则表达式(@“([0-9]+),([0-9]+)”; 匹配m=r.Match(str); var points=新列表(); while(m.Success) { int x,y; if(Int32.TryParse(m.Groups[1].Value,out x)和&Int32.TryParse(m.Groups[2].Value,out y)) { 点。添加(新系统。图纸。点(x,y)); } m=m.NextMatch(); }
您可以尝试使用
Regex
解析它:

string str = @"(11,22)(2,3)(4,-10)(5,0)";
Regex r = new Regex(@"(-?[0-9]+),(-?[0-9]+)");
Match m = r.Match(str);
var points = new List<System.Drawing.Point>();
while (m.Success)
{
    int x, y;
    if (Int32.TryParse(m.Groups[1].Value, out x) && Int32.TryParse(m.Groups[2].Value, out y))
    {
        points.Add(new System.Drawing.Point(x, y));
    }
    m = m.NextMatch();
}
字符串str=@“(11,22)(2,3)(4,-10)(5,0)”; 正则表达式r=新正则表达式(@“([0-9]+),([0-9]+)”; 匹配m=r.Match(str); var points=新列表(); while(m.Success) { int x,y; if(Int32.TryParse(m.Groups[1].Value,out x)和&Int32.TryParse(m.Groups[2].Value,out y)) { 点。添加(新系统。图纸。点(x,y)); } m=m.NextMatch(); }
这就是语法吗?@Zencoder这适用于System.Windows.Point,但不适用于System.Drawing.Point。这正是语法吗?@Zencoder这适用于System.Windows.Point,但不适用于System.Drawing.Point。