0

i need to extract width and height values from a very long xaml string. What i would like to do: I want to match the part that has the values with a regular expression and the match to already have the values in separate groups, so i don't have to parse the string anymore. My regex looks like this:

Match pagesizes = Regex.Match(str, "SomeElement\\S\\s\\S+(?<width>\\d+)\\S\\s\\S+(?<height>\\d+)");

while the string looks like this (that's just a small part of the whole string):

Uniform\" Name=\"view\"><av:Canvas Name=\"SomeElement\" Width=\"2100\" Height=\"2970\"><av:Line X1=\"0\" Y1=\"1485\" X2=\"150\" Y2=\"1485\" Stroke=\"#FF000000\" StrokeThickness=\"1\" Name=\"holepunchline\" /><av:TextBlock Text=\"programname\" FontSize=\"30\" Foreground=\"#FF808080\" Name=\"text\" av:Canvas.Left=\"160\" av:Canvas.Bottom=\"100\"><av:TextBlock.LayoutTransform><av:RotateTransform Angle=\"-90\" /></av:TextBlock.LayoutTransform></av:TextBlock><av:Grid Name=\"grid\" Width=\"1850\" Height=\"2720\" 

Pagesize contains the string i want and has three groups. But the groups width and height remain empty. I'm fairly inexperienced in using regular expressions. Where is my error?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Myrkjartan
  • 166
  • 3
  • 16
  • You are using `\S+` that is greedy, you may use `@"SomeElement\S\s\S+?(?\d+)\S\s\S+?(?\d+)"`, but since it is XML, you should just parse it with XML parser. – Wiktor Stribiżew Apr 22 '20 at 11:45
  • The questionmark helped. Thx. The string itself is actually a WPF object i serialized with XamlWriter. Now i want to make a WPF element from the serialized string again, but these sizes i need are not being set corectly. That's why i need to get them this way. – Myrkjartan Apr 22 '20 at 11:53

0 Answers0