CSharpNewBee
CSharpNewBee

Reputation: 1981

Create a single string using regex

How do i read the following text into a single string using c# regex?

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

this is stored in a c# List object

so for example, the above needs to return

this is a string this is continuation of string even more tex

I've thought something like this would do the job, but it doesn't return any group values

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;
 }

perhaps, i'm going about this all wrong, but i need to use the regex expression to formualte a single string from the example string.

Upvotes: 0

Views: 96

Answers (1)

dav_i
dav_i

Reputation: 28107

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

Explanation of regex:

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

Alternatively you could use the regex [^A-Za-z\s] which reads "don't match capital letters, lowercase letters or whitespace".

Upvotes: 2

Related Questions