[英]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. 如何在C#中编写正则表达式,以仅获取未与数字,特殊字符连接的字母单词。
I have following text " About Time (2013) [1080p] " and want the output like About Time 我有以下文本“ About Time(2013)[1080p] ”,并希望输出诸如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 像这样回来回来关于时间p
If you want to do without Regex
如果您想不使用正则
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) [1]将字符串拆分为单词(以空格分隔)
[2] Takes only those words where all characters are letter [2]仅采用所有字符均为字母的单词
[3] Joins them back with space as separator [3]使用空格作为分隔符将它们重新连接起来
you can check the word bounderies with \\b 您可以使用\\ b检查单词边界
\b[a-zA-Z]+\b
but this way you still find the words bewteen round brackets or square brackets 但是通过这种方式,您仍然可以在圆括号或方括号之间找到单词
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.