1

我创建了一个 XAML 文档,其中包含用于呈现文本的字形。我实际上想使用 TextBlocks 来呈现这些,所以我想将结果字符串从

<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=" />

进入

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

我怎样才能做到这一点?

4

1 回答 1

1

描述

此正则表达式将要求字形块包含 unicodestring、fonturi 和填充属性。这些属性可以以任何顺序出现,正则表达式也会跳过可能被误认为属性的值。

正则表达式

<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

用。。。来代替

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

在此处输入图像描述

C#.net 示例

输入文本

<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=" />

代码

使用系统;

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

更换后

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

捕获组

[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"

免责声明

我不能对字体做任何事情,我看到你评论说字体需要后处理。我正在捕获 FontUri 属性以提供帮助。

于 2013-06-28T14:27:31.043 回答