正则表达式用于删除 c# 中两个字符之间的文本

本文关键字:两个 字符 之间 文本 删除 用于 正则表达式 | 更新日期: 2023-09-27 18:37:12

>我有以下字符串,我需要删除 =select 和以下 } 字符之间的所有内容

前任。输入类型:=从电缆中选择前 10 种类型}

最终结果是字符串变量只显示输入类型:

我一直在寻找一种使用 Regex 执行此操作的方法,但我也对其他方法持开放态度。 提前感谢您的帮助。

正则表达式用于删除 c# 中两个字符之间的文本

string input = "Enter Type:=select top 10 type from cable}";
System.Text.RegularExpressions.Regex regExPattern = new System.Text.RegularExpressions.Regex("(.*):=select.*}");
System.Text.RegularExpressions.Match match = regExPattern.Match(input);
string output = String.Empty;
if( match.Success)
{
    output = match.Groups[1].Value;
}
Console.WriteLine("Output = " + output);
"

output"变量的值将是输入字符串的":=select"段之前找到的值。 如果您需要从输入字符串中提取其他信息,请将其括起来,找到的匹配项将添加到匹配项中。组数组。 顺便说一下,匹配的价值。组[0]。值是原始字符串。

var rx = new Regex("=select[^}]*}");;
Console.WriteLine(rx.Replace ("Enter Type:=select top 10 type from cable}", ""));

Regexp.Replace(字符串输入,字符串输出)函数用字符串"output"替换所有与给定正则表达式匹配的子字符串。第一行定义正则表达式,匹配 =select 和 } 之间的所有内容