如何仅解析字符串中的数字? [英] How can i parse only the numbers from the string?

查看:87
本文介绍了如何仅解析字符串中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在transform属性下的Rect.rectangles中具有字符串"matrix(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)"来具有2个属性,例如transform和transform1,并且每个属性都有一个数字例如:转换"0.12345678" transform1"1.12345678"

I want that in Rect.rectangles under the transform property instead to have the string "matrix(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)" to have 2 properties like transform and transform1 and each one will have a number for example: transform "0.12345678" transform1 "1.12345678"

private void Parse()
    {
        XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg");

        Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
        {
            style = Rect.GetStyle((string)x.Attribute("style")),
            id = (string)x.Attribute("id"),
            width = (double)x.Attribute("width"),
            height = (double)x.Attribute("width"),
            x = (double?)x.Attribute("width"),
            y = (double?)x.Attribute("width"),
            transform = x.Attribute("transform".Substring(18, 43)) == null ? "" : (string)x.Attribute("transform".Substring(18, 43))
        }).ToList();
    }

和Rect类:

public class Rect
    {
        public static List<Rect> rectangles { get; set; }
        public Dictionary<string, string> style { get; set; }
        public string id { get; set; }
        public double width { get; set; }
        public double height { get; set; }
        public double? x { get; set; }
        public double? y { get; set; }
        public string transform { get; set; }

        public static Dictionary<string, string> GetStyle(string styles)
        {
            string pattern = @"(?'name'[^:]+):(?'value'.*)";
            string[] splitArray = styles.Split(new char[] { ';' });
            Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
                .GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
            return style;
        }
    }

我正在尝试使用子字符串,但超出范围异常.

I'm trying to use substring but getting out of range exception.

ArgumentOutOfRangeException:不能超过字符串的长度

ArgumentOutOfRangeException: Cannot exceed length of string

在线:

transform = x.Attribute("transform".Substring(18, 43)) == null ? "" : (string)x.Attribute("transform".Substring(18, 43))

字符串是:

transform ="matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)"

transform="matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)"

我只想获得前两个数字:0.40438612,-0.91458836

And i want to get only the first two numbers: 0.40438612,-0.91458836

更新:

我也找到了一种使用foreach的方法:

I found also a way to do it using foreach:

private void Parse()
    {
        XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg");

        Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
        {
            style = Rect.GetStyle((string)x.Attribute("style")),
            id = (string)x.Attribute("id"),
            width = (double)x.Attribute("width"),
            height = (double)x.Attribute("width"),
            x = (double?)x.Attribute("width"),
            y = (double?)x.Attribute("width"),
            transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
        }).ToList();
        string t = null;
        foreach(Rect rect in Rect.rectangles)
        {
            if (rect.transform != null && rect.transform != "")
            {
                t = rect.transform.Substring(7, 21);
            }
        }
    }

但是我不想在新的Rect(){ 我想在行上提取前两个数字:

But i don't want to do it after the new Rect() { I want to do the first two numbers extraction on the line:

transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")

原因是我在代码底部有一个类:

The reason is that i have in the bottom of the code a class:

public class Rect
    {
        public static List<Rect> rectangles { get; set; }
        public Dictionary<string, string> style { get; set; }
        public string id { get; set; }
        public double width { get; set; }
        public double height { get; set; }
        public double? x { get; set; }
        public double? y { get; set; }
        public string transform { get; set; }

        public static Dictionary<string, string> GetStyle(string styles)
        {
            string pattern = @"(?'name'[^:]+):(?'value'.*)";
            string[] splitArray = styles.Split(new char[] { ';' });
            Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
                .GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
            return style;
        }
    }

而且我希望属性转换已包含/包含两个数字.

And i want that the property transform to hold/contain the two numbers already.

另一个小问题是,如果数字中的一个在开始时具有-(减号),例如,如果transform包含字符串,该如何提取前两个数字:"matrix(0.40438612,-0.91458836,0.92071162,0.39024365, 0,0)"

And another small problem how can i extract the first two numbers if for example one of the numbers have -(minus) in the start for example if transform contain the string: "matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)"

如果两个数字之一以负号开头或两个都以负号开头,则Substring(7,21)将不起作用.无论如何,我都需要某种方式来获得前两个数字.但是主要的问题是如何直接从属性中直接提取数字.

If one of the two numbers have minus in the start or both of them have minus in the start the Substring(7, 21) will not work. I need somehow to get in any case the first two numbers. But the main issue is how to extract the numbers directly from the attribute already.

推荐答案

您是否正在寻找正则表达式?

 using System.Globalization;
 using System.Linq;
 using System.Text.RegularExpressions;

 ... 

 string transform = "matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)";

 double[] result = Regex
   .Matches(transform, @"-?[0-9]*\.?[0-9]+")
   .OfType<Match>()
   .Take(2)
   .Select(match => double.Parse(match.Value, CultureInfo.InvariantCulture))
   .ToArray(); 

这篇关于如何仅解析字符串中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆