2017-06-03 14 views
-1

私はRect.rectangles内の文字列「マトリックス(0.87142591,0.49052715、-0.49052715,0.87142591,0,0)」2つの変換などのプロパティとtransform1を持っているし、それぞれの番号を持つことになりますを持っている代わりに、transformプロパティを下にしたいです例えば:私は部分文字列を使用しようとしているが、範囲例外のうち、取得してい文字列の数値だけを解析するにはどうすればよいですか?

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; 
     } 
    } 

: "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クラスを変換します。

例外ArgumentOutOfRangeException:行で文字列

の長さを超えることはできません:

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

文字列がある:

は、= "マトリックス(0.40438612、-0.91458836,0.92071162,0.39024365変換0,0)」

と私は上取得したいですLY最初の2つの数字:0.40438612、-0.91458836

更新:

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); 
      } 
     } 
    } 

しかし、私は新しいの後にそれを行うにはしたくない:

私はまたのforeachを使用してそれを行うための方法を見つけましたRECT(){ 私はライン上の最初の2つの数字の抽出をしたい:

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

理由は、私は、コードクラスの下に持っているということです。

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; 
     } 
    } 

そして、私は、プロパティの変換がすでに2つの数値を保持/保持したいと思っています。

もう1つの小さな問題は、最初の2つの数値を抽出するにはどうすればよいのでしょうか?たとえば、最初の2つの数値に - (マイナス) 、0,0)」

2つの数の1が開始またはそれらの両方持っているマイナスサブストリング(7、21)が動作しません起動にマイナス持っている場合。私は何とか最初の2つの数字を取得する必要があります。しかし、主な問題は、属性から直接数値を抽出する方法です。

+2

最良の答えですpositiion 18で始まり、9文字の長さの文字列 '' transform "'から長さ43を持ちます。 –

答えて

2

あなたは正規表現をお探しですか?

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(); 
+0

しかし、私はそれをtransform = x.Attribute( "transform" .Substring(18,43))== nullと一緒に使用しますか? "":(文字列)x.Attribute( "transform" .Substring(18,43))私は属性上でそれをしたい。 –

1

は正規表現がなければ、あなたは最初の括弧の直後に始まる文字列の一部を取得して、コンマを使用して、それを分割することができます:

var transform="matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)"; 
var numbers = transform.Substring(transform.IndexOf('(') + 1).Split(','); 
Console.WriteLine(double.Parse(numbers[0])); 
Console.WriteLine(double.Parse(numbers[1])); 

エラー処理を追加してください。

<== Fiddle Me ==>

0

ソリューション:ここ

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(); 

     for(int i = 0; i < Rect.rectangles.Count; i++) 
     { 
      if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "") 
      { 
       Rect.rectangles[i].transform = Rect.rectangles[i].transform.Substring(7, 21); 
      } 
     } 
    } 
0

は ` "変換" .Substring(18、43)`あなたはサブよりを取得しようとしているで

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      Rect.rectangles = doc.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 ? null : (object)Rect.GetTransform((string)x.Attribute("transform")) 
      }).ToList(); 


     } 
    } 
    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 object 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; 
     } 
     public static KeyValuePair<double, double> GetTransform(string matrix) 
     { 
      string pattern = @"[-+]?\d+\.\d+"; 
      MatchCollection matches = Regex.Matches(matrix, pattern); 
      KeyValuePair<double, double> kp = new KeyValuePair<double, double>(
       double.Parse(matches[0].Value), 
       double.Parse(matches[0].Value) 
       ); 

      return kp; 
     } 
    } 
} 
関連する問題