我有以下字符串:
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。我如何使用 SubString
或 Linq
或 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/