Chris
Chris

Reputation: 27384

Replace well formed XML tags

I have a XAML document that is created which contains Glyphs to render texts. I actually want to use TextBlocks to render these instead so I want to turn the resultant string from

<Glyphs" UnicodeString="Test string" Fill="#ff000000" FontUri="http://localhost:20450/simplesilverlightxpsviewer/Intro.xps/96081314-55DD-46FB-BF29-F998D2A1B156.odttf" FontRenderingEmSize="21.2397" StyleSimulations="None" OriginX="144" OriginY="534.56" Indices=" />

into

<TextBlock Text="Test String" Font="Arial" Foreground="#ff000000" />

How can I achieve this?

Upvotes: 1

Views: 144

Answers (1)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

This regex will require the glyph block to contain a unicodestring, fonturi, and fill attributes. These attributes can appear in any order, the regex will also skip over values which may be mistaken as attributes.

Regex

<Glyphs"(?=\s) # capture the open tag
(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sUnicodeString=('[^']*'|"[^"]*"|[^'"][^\s>]*))  # get the UnicodeString attribute
(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sFontUri=('[^']*'|"[^"]*"|[^'"][^\s>]*)) # get the fonturi attribute
(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sFill=('[^']*'|"[^"]*"|[^'"][^\s>]*)) get the fill attribute
(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sFontRenderingEmSize=('[^']*'|"[^"]*"|[^'"][^\s>]*)) # get the FontRenderingEmSize attribute
[^>]*"\s\/> # get the entire tag

Replace with

<textblock text=$1 font="arial" foreground=$3 />

enter image description here

C#.net Example

Input Text

<Glyphs" NotRealAttribute=' UnicodeString="do not match" ' UnicodeString="Test string" Fill="#ff000000" FontUri="http://localhost:20450/simplesilverlightxpsviewer/Intro.xps/96081314-55DD-46FB-BF29-F998D2A1B156.odttf" FontRenderingEmSize="21.2397" StyleSimulations="None" OriginX="144" OriginY="534.56" Indices=" />

Code

using System;

using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          String matchpattern = @"<Glyphs""(?=\s)
(?=(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*?\sUnicodeString=('[^']*'|""[^""]*""|[^'""][^\s>]*))
(?=(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*?\sFontUri=('[^']*'|""[^""]*""|[^'""][^\s>]*))
(?=(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*?\sFill=('[^']*'|""[^""]*""|[^'""][^\s>]*))
(?=(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*?\sFontRenderingEmSize=('[^']*'|""[^""]*""|[^'""][^\s>]*))
[^>]*""\s\/>";
          String replacementpattern = @"<textblock text=$1 font=""arial"" foreground=$3 />";
          Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern,RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.Singleline));
        }
    }
}

After Replacement

<textblock text="Test string" font="arial" foreground="#ff000000" />

Capture Groups

[0] => <Glyphs" NotRealAttribute=' UnicodeString="do not match" ' UnicodeString="Test string" Fill="#ff000000" FontUri="http://localhost:20450/simplesilverlightxpsviewer/Intro.xps/96081314-55DD-46FB-BF29-F998D2A1B156.odttf" FontRenderingEmSize="21.2397" StyleSimulations="None" OriginX="144" OriginY="534.56" Indices=" />
[1] => "Test string"
[2] => "http://localhost:20450/simplesilverlightxpsviewer/Intro.xps/96081314-55DD-46FB-BF29-F998D2A1B156.odttf"
[3] => "#ff000000"
[4] => "21.2397"

Disclaimer

I can't do anything with the font, and I see you commented that font would need to be post processed. I'm capturing the FontUri attribute to assist.

Upvotes: 1

Related Questions