0

如何使用 c# 正则表达式将以下文本读入单个字符串?

* EDIT * :70://this is a string //this is continuation of string even more text 13

这存储在 ac#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;
 }

也许,我做错了,但我需要使用正则表达式从示例字符串中格式化单个字符串。

4

1 回答 1

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]“不匹配大写字母、小写字母或空格”的正则表达式。

于 2013-09-11T11:21:08.200 回答