Home > Software engineering >  Regex .Net Match with only letters and spaces
Regex .Net Match with only letters and spaces

Time:03-23

I was attempting to create a .net regex to extract all letters and whitespace however my attempts weren't working.

Input String:

2034Late Summer2304

Desired Output String:

Late Summer

What I've tried:

^(?! )[A-Za-z\s] $
^[a-zA-Z\s]*$
^[a-zA-Z ]*$

Any help would be greatly appreciated.

If possible, if it could grab strings that have whitespace that is surrounded by letters, then even better. (I.e. doesn't match every whitespace).

CodePudding user response:

How about replacing everything you don't want to keep with ""?

replace("2034Late Summer2304","[^A-Za-z\s]","")

CodePudding user response:

Try this C# code to extract the middle part skipping digits.

string pattern = "([a-zA-Z\\s] )";
string input = "2034Late Summer2304";
var m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
if (m.Success) Console.WriteLine(m.Value);

The output would be the following:

Late Summer
  • Related