我创建了一个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" />
我怎样才能做到这一点?
答案 0 :(得分:1)
此正则表达式将要求字形块包含unicodestring,fonturi和fill属性。这些属性可以按任何顺序出现,正则表达式也会跳过可能被误认为属性的值。
<强>正则表达式强>
<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 />
输入文字
<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=" />
<强>代码强>
使用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));
}
}
}
替换后
<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属性以提供帮助。