c# - 删除字符串从一个单词开始直到字符

标签 c# regex string linq

我有以下字符串:

customer.Id as CustomerID, case when customer.Name...

我想从我的字符串中删除 as CustomerID,即所有以 as 开头并以 , 结尾的句子。我怎样才能做到这一点。我和 Linq 一起去了,但不幸的是 take 似乎不起作用:

select(c => c.TakeWhile(ch => ch!= 'As').SkipWhile(c=>c != ','))

如您在 TakeWhile 中所见,我可以使用 char 而不是 word。我如何使用 SubStringLinq 或 Regex 执行此操作?

最佳答案

你可以使用

var s = "customer.Id as CustomerID, case when customer.Name...";
var res = Regex.Replace(s, @"\s+as\s+[^,]+","");
Console.WriteLine(res);
// => customer.Id, case when customer.Name...

参见 C# demo

正则表达式 ( see demo) 是

\s+as\s+[^,]+

它匹配:

  • \s+ - 1+ 个空格
  • as - 单词 as
  • \s+ - 1+ 个空格
  • [^,]+ - 除了 ,
  • 之外的 1+ 个字符

关于c# - 删除字符串从一个单词开始直到字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47767459/

相关文章:

C# WPF 属性网格文件浏览器

c# - 如何正确保留标识值以供在数据库中使用?

c# - 如何使用 LINQ 解析 OWL 文件(包含 RDF namespace )

用于带括号的单词边界检测的javascript正则表达式

c# - MailMerge #foreach 使用 Aspose.Words 的字符串列表

c# - 关于 SyncRoot 模式 : what is the correct way to use this pattern? 的一些说明

Ruby 正则表达式,有没有办法只匹配文字匹配?

javascript - 正则表达式仅引用字符串匹配(而不是数字)

php vs javascript 处理信息的速度

string - python 修改内置字符串函数