C# 使用正则表达式创建单个字符串

C# 使用正则表达式创建单个字符串,c#,regex,C#,Regex,如何使用c#regex将以下文本读入单个字符串 *编辑* :70://这是一个字符串 //这是我的继续 字符串甚至更多的文本13 这存储在c#List对象中 例如,上面的内容需要返回 this is a string this is continuation of string even more tex 我认为这样做可以完成任务,但它不会返回任何组值 foreach (string in inputstring) { string[] words words = str.Spli

如何使用c#regex将以下文本读入单个字符串

*编辑* :70://这是一个字符串 //这是我的继续 字符串甚至更多的文本13

这存储在c#List对象中

例如,上面的内容需要返回

this is a string this is continuation of string even more tex
我认为这样做可以完成任务,但它不会返回任何组值

foreach (string in inputstring)
{
   string[] words
   words = str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
   foreach (string word in words)
   {
      stringbuilder.Append(word + " ");
   }
 }
 Match strMatch = Regex.Match(stringBuilder, @"[^\W\d]+");
 if(strMatch.Success)
 {
     string key = strMatch.Groups[1].Value;
 }
也许,我完全错了,但我需要使用正则表达式从示例字符串中格式化单个字符串

var input = @":70://this is a string //this is continuation of string even more text 13";

Regex.Replace(input, @"[^\w\s]|[\d]", "").Trim();
// returns: this is a string this is continuation of string even more text
正则表达式的解释:

[^ ... ] = character set not matching what's inside
\w       = word character
\s       = whitespace character
|        = or
\d       = digit

或者,您也可以使用regex
[^A-Za-z\s]
,它的内容是“不匹配大写字母、小写字母或空格”。

此代码不会编译-您的
foreach
中缺少字符串的变量名。另外,无论如何都不要使用上述变量搜索替换任何非字母字符的正则表达式,即[a-zA-Z],包括回车符和换行符。通过用string.empty替换所有不需要的代码,您应该可以实现您的目标。有点像这样的问题:我没有列出所有代码,所以我不太担心它不会编译这只是一个示例代码-嘿,但感谢负号:-)出于某种原因,示例字符串只出现在一行上,这可能会导致混淆和一些响应。检查单个字符串的正则表达式将很容易。