写一个去除AI2XAML注释及多余数字位数的WPF窗体程序
2008-04-04 14:46
423 查看
使用正则表达式去除多余注释及冗余数字位,关键代码:
string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
temp = Regex.Replace(temp, @"<!--.*-->", "");
temp = Regex.Replace(temp, @"[/s| ]*/r","");
运行效果截图:
// ReplaceWindow.xaml
<Window x:Class="BrawDraw.Com.AIXamlConverter.ReplaceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800">
<Grid>
<Label Height="28" Margin="45,17,219,0" Name="label1" VerticalAlignment="Top" Foreground="Red">将XAML内容复制到下面文本框内,然后点替换,在“结果”文本框中得到新内容。</Label>
<Button Height="23" HorizontalAlignment="Right" Margin="0,17,119,0" Name="btnReplace" VerticalAlignment="Top" Width="75" Click="btnReplace_Click">替换</Button>
<TextBox Margin="124,45,26,0" Name="txtBoxOldContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Height="151" VerticalAlignment="Top" />
<TextBox Margin="124,200,26,201" Name="txtBoxResultContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />
<Label Height="28" HorizontalAlignment="Left" Margin="45,45,0,0" Name="label2" VerticalAlignment="Top" Width="91">替换前内容:</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="45,200,0,0" Name="label3" VerticalAlignment="Top" Width="91">结果:</Label>
<StackPanel Margin="123,0,27,12" Height="180" VerticalAlignment="Bottom">
<Border BorderBrush="Black" BorderThickness="1" Height="180" Name="borderResult">
<StackPanel Name="spResult" />
</Border>
</StackPanel>
<Label Height="28" HorizontalAlignment="Left" Margin="45,0,0,164" Name="label4" VerticalAlignment="Bottom" Width="91">效果预览:</Label>
</Grid>
</Window>
// ReplaceWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Text.RegularExpressions;
namespace BrawDraw.Com.AIXamlConverter
{
/// <summary>
/// Interaction logic for ReplaceWindow.xaml
/// </summary>
public partial class ReplaceWindow : Window
{
public ReplaceWindow()
{
InitializeComponent();
}
private void btnReplace_Click(object sender, RoutedEventArgs e)
{
string oldContent = txtBoxOldContent.Text;
//string pattern = @"(/d+)/.(/d)/d{5,}([/,/s]?)";
string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
temp = Regex.Replace(temp, @"<!--.*-->", "");
temp = Regex.Replace(temp, @"[/s| ]*/r","");
txtBoxResultContent.Text = temp;
try
{
//下面的代码用于预览效果显示:
System.IO.StringReader stringReader = new System.IO.StringReader(temp);
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
UIElement uie = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);
spResult.Children.Add(uie);
borderResult.BorderBrush = Brushes.Transparent;
}
catch (Exception exc)
{
MessageBox.Show(string.Format("出错了,错误是:/r/n{0}", exc.Message));
}
}
}
}
string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
temp = Regex.Replace(temp, @"<!--.*-->", "");
temp = Regex.Replace(temp, @"[/s| ]*/r","");
运行效果截图:
// ReplaceWindow.xaml
<Window x:Class="BrawDraw.Com.AIXamlConverter.ReplaceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800">
<Grid>
<Label Height="28" Margin="45,17,219,0" Name="label1" VerticalAlignment="Top" Foreground="Red">将XAML内容复制到下面文本框内,然后点替换,在“结果”文本框中得到新内容。</Label>
<Button Height="23" HorizontalAlignment="Right" Margin="0,17,119,0" Name="btnReplace" VerticalAlignment="Top" Width="75" Click="btnReplace_Click">替换</Button>
<TextBox Margin="124,45,26,0" Name="txtBoxOldContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Height="151" VerticalAlignment="Top" />
<TextBox Margin="124,200,26,201" Name="txtBoxResultContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />
<Label Height="28" HorizontalAlignment="Left" Margin="45,45,0,0" Name="label2" VerticalAlignment="Top" Width="91">替换前内容:</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="45,200,0,0" Name="label3" VerticalAlignment="Top" Width="91">结果:</Label>
<StackPanel Margin="123,0,27,12" Height="180" VerticalAlignment="Bottom">
<Border BorderBrush="Black" BorderThickness="1" Height="180" Name="borderResult">
<StackPanel Name="spResult" />
</Border>
</StackPanel>
<Label Height="28" HorizontalAlignment="Left" Margin="45,0,0,164" Name="label4" VerticalAlignment="Bottom" Width="91">效果预览:</Label>
</Grid>
</Window>
// ReplaceWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Text.RegularExpressions;
namespace BrawDraw.Com.AIXamlConverter
{
/// <summary>
/// Interaction logic for ReplaceWindow.xaml
/// </summary>
public partial class ReplaceWindow : Window
{
public ReplaceWindow()
{
InitializeComponent();
}
private void btnReplace_Click(object sender, RoutedEventArgs e)
{
string oldContent = txtBoxOldContent.Text;
//string pattern = @"(/d+)/.(/d)/d{5,}([/,/s]?)";
string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
temp = Regex.Replace(temp, @"<!--.*-->", "");
temp = Regex.Replace(temp, @"[/s| ]*/r","");
txtBoxResultContent.Text = temp;
try
{
//下面的代码用于预览效果显示:
System.IO.StringReader stringReader = new System.IO.StringReader(temp);
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
UIElement uie = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);
spResult.Children.Add(uie);
borderResult.BorderBrush = Brushes.Transparent;
}
catch (Exception exc)
{
MessageBox.Show(string.Format("出错了,错误是:/r/n{0}", exc.Message));
}
}
}
}
相关文章推荐
- c程序:打印出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身
- 编写一个程序,求出满足下列条件的四位数:该数是个完全平方数,且第一、三位数字之和为10,第二、四位数字之积为12
- 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如: 153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。 1.程序分析:利用f
- 窗体标题为“我文本框实验”;窗体上一个标签,内容如图;窗体上有一个文本框,文本框只能输入0至9这十种数字,且最多输入8个数字;单击结束按钮程序即可结束。
- 42.编写程序打印所有的“水仙花数”。“水仙花数”指一个三位数,其各位数字立方和等于该数本身,例如153是一个“水仙花数”,因为 153=1×1×1+3×3×3+5×5×5。
- 编写一个Java应用程序。用户从键盘输入一个1—9999之间的数,程序将判断这个数是几位数, 并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同, 例如12121、3
- 编写一个Java应用程序。用户从键盘输入一个1—9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、3223都
- 一个去除视频前景,保留背景的 opencv 小程序 (附注释)
- C#源代码—编写一个程序求出所有的“水仙花数”。“水仙花数”是指一个3位数,其各位数字的立方和恰好等于该数本身。例如153=1*1*1+5*5*5+3*3*3,所以153是“水仙花数”。
- C---某人岁数的3次方是四位数,4次方是六位数,且知此人岁数的3次方和4次方用遍了0~9十个数字。编写一个程序求此人的岁数
- 【程序3】题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位 数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次 方+5的三次方+3的三次方。
- 【程序24】TestNumber.java 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
- 中软面试(一):java写一个随机生成四位数的程序 每位数字不重复
- 2. 编写一个Java应用程序,用户从键盘输入一个1~9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、32
- Linux_C练习:编写一个程序,求出满足下列条件的四位数:该数是个完全平方数,且第一、三位数字之和为10,第二、四位数字之积为12;
- 【程序24】 题目:给一个不多于5位的正整数,要求: 一、求它是几位数,二、逆序打印出各位数字。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
- JAVA算法题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
- 编写一个删除C语言程序中所有注释语句的程序