Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式-删除匹配的正则表达式_C#_Regex - Fatal编程技术网

C# 正则表达式-删除匹配的正则表达式

C# 正则表达式-删除匹配的正则表达式,c#,regex,C#,Regex,如何从字符串中删除应用的正则表达式并恢复原始字符串 我有绳子 12345679 应用正则表达式后,我得到了字符串 “123-456+789” 在我的代码中,我有不同类型的正则表达式,我想找到与当前字符串匹配的正则表达式,并将其转换回原始格式 我可以使用 Regex.IsMatch() 但如何获得原始字符串 这是一段代码 /// <summary> /// Initializes a new instance of the <see cref="Ma

如何从字符串中删除应用的正则表达式并恢复原始字符串

我有绳子

12345679

应用正则表达式后,我得到了字符串

“123-456+789”

在我的代码中,我有不同类型的正则表达式,我想找到与当前字符串匹配的正则表达式,并将其转换回原始格式

我可以使用

Regex.IsMatch() 
但如何获得原始字符串

这是一段代码

     /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
          this.InitializeComponent();
          this.patientIdPattern = @"^(\d{3})(\d{3})(\d{3})";
          this.patientIdReplacement = "$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.patientIdPattern, this.patientIdReplacement);
          lblFormatted.Content = formatted;
        }


public void GetOriginalString(string formattedString)
{
//// Now here i want to convert formatted string back to original string.
}
//
///初始化类的新实例。
/// 
公共主窗口()
{
this.InitializeComponent();
this.patientIdPattern=@“^(\d{3})(\d{3})(\d{3})”;
this.patientidplacement=“$1-$2+$3”;
}
/// 
///处理UIElement控件的OnLostFocus事件。
/// 
///事件的来源。
///包含事件数据的实例。
私有void UIElement_OnLostFocus(对象发送方,路由目标)
{
string formatted=Regex.Replace(textBox.Text,this.patientIdPattern,this.patientidplacement);
lblFormatted.Content=已格式化;
}
public void GetOriginalString(字符串格式化字符串)
{
////现在我想把格式化的字符串转换回原始字符串。
}

完全匹配始终位于
Regex.Matches
返回的
MatchCollection
的索引0处

试试这个:

string fullMatch = "";

var matchCollection = Regex.Matches(originalText, regex);

foreach(var match in matchCollection)
{
   fullMatch = match.Groups[0].Value;
   break;
}

除了自己保存一份原始输入外,没有办法要求
Regex
返回原始输入。因为,取决于您如何使用它,
Regex.Replace
可能是一种单向转换。你的选择是:

  • 保留一份原件
  • 手工制作某种方法来恢复转换。这不可能神奇地完成

  • 您不能在原始字符串的副本上使用正则表达式吗?;)保存一份原件的副本?你能举一个字符串的例子吗;2) 与此字符串匹配的正则表达式;3) 将此正则表达式应用于字符串的代码和4)正则表达式的结果?
    Regex.Replace("123-456+789", @"^(\d{3})\-(\d{3})\+(\d{3})", "$1$2$3")