简体   繁体   中英

how to write regular expression in C# to get only alphabets words

how to write regular expression in C# to get only alphabets words that is not concatenated with numbers,special characters.
I have following text " About Time (2013) [1080p] " and want the output like About Time

This is what I have tried

string myString="About Time (2013) [1080p]";
Regex.Replace(myString, @"[^a-zA-Z]+", " ");

It's returning as like this About Time p

If you want to do without Regex

string myString = "About Time (2013) [1080p]";
var words = myString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var alphaWords = string.Join(" ", words.Where(w => w.All(Char.IsLetter)));

What it does is

[1] Splits the string into words (separated by space)
[2] Takes only those words where all characters are letter
[3] Joins them back with space as separator

you can check the word bounderies with \\b

\b[a-zA-Z]+\b

but this way you still find the words bewteen round brackets or square brackets

example here

string myString="About Time (2013) [1080p]";
Regex.Replace(myString, @"\S*[^a-zA-Z\s]\S*", "");
private static string FilterTest(string input) => string
                      .Join(" ", (new Regex(@"[a-z]+",RegexOptions.IgnoreCase))
                      .Matches(input)
                      .Cast<Match>().Select(e => e.Value));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM