使用正则表达式去除多余注释及冗余数字位,关键代码:
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
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800">
// 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
{
///
/// Interaction logic for ReplaceWindow.xaml
///
public partial class ReplaceWindow : Window
{
public ReplaceWindow()
{
InitializeComponent();
}
private void btnReplace_Click(object sender, RoutedEventArgs e)
{
string ldContent = 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));
}
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14601556/viewspace-528541/ ,如需转载,请注明出处,否则将追究法律责任。