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

时间:2013-09-11 11:12:45

标签: 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.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;
 }
也许,我说这一切都错了,但我需要使用正则表达式从示例字符串中形成一个字符串。

1 个答案:

答案 0 :(得分:2)

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

或者,您可以使用正则表达式[^A-Za-z\s],其中显示“不匹配大写字母,小写字母或空格”。