gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:16:50 25 4
gpt4 key购买 nike

我有以下字符串:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com