正则表达式 - 删除正则表达式

时间:2014-05-30 04:39:26

标签: c#

我使用正则表达式来获取特定格式的字符串。经过多次操作后,我想要原来的字符串。

现在我只使用了一种模式,我正在考虑使用更多模式,我想确定哪种模式应用于格式化字符串并将其恢复为原始字符串。

这是我的一段代码,

string formattedId=string.empty;

 /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
          this.InitializeComponent();
          this.idPattern = @"^(\d{3})(\d{3})(\d{3})";
          this.idReplacement = "$1-$2+$3";
        }

        /// <summary>
        /// Handles the OnLostFocus event of the UIElement control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void UIElement_OnLostFocus(object sender, RoutedEventArgs e)
        {
          string formatted = Regex.Replace(textBox.Text, this.idPattern , this.idReplacement );
          formattedId = formatted;
        }


public string GetOriginalString()
{
//// Now here i want to convert formatted string back to original string.
}

例如123456789字符串将格式化为123-456 + 789 在此之后,我想检查应用了哪种模式并以原始格式返回字符串

1 个答案:

答案 0 :(得分:1)

创建一个全局字符串并将文本框值放在该字符串中。 查看orignal字符串只需使用该全局字符串:

   string formattedId=string.Empty;
   string OriginalStr=string.Empty; 
   public MainWindow()
   {
      this.InitializeComponent();
      this.idPattern = @"^(\d{3})(\d{3})(\d{3})";
      this.idReplacement = "$1-$2+$3";
   }

   private void UIElement_OnLostFocus(object sender, RoutedEventArgs e)
   {
       OriginalStr=Convert.ToString(textBox.Text);
       string formatted = Regex.Replace(textBox.Text, this.idPattern , this.idReplacement );
       formattedId = formatted;
   }

我希望这会有所帮助