Regex -删除正则表达式

本文关键字:正则表达式 删除 Regex | 更新日期: 2023-09-27 18:05:55

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

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

这是我的代码,

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在此之后,我想检查应用了哪个模式并将字符串恢复为原始格式

Regex -删除正则表达式

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

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

希望对大家有所帮助