当前位置: 动力学知识库 > 问答 > 编程问答 >

C# Regex Help

问题描述:

I am having a few problems with regex in C#. I require a string to be passed in and only the letters to be returned (as a string), so for example if the string is "4hr", I want "hr" to be returned. If the string is "Gp. 23", I just want "Gp" to be returned.

I've tried :

 string[] extractedWords = System.Text.RegularExpressions.Regex.Split(expr, "[a-zA-Z]");

But that doesnt seem to work.

网友答案:

If you want just a string to be returned, using split is a bad idea. How about:

string filtered = Regex.Replace(expr, "[^A-Za-z]", "");

In other words "replace anything that isn't A-Z or a-z with an empty string". Note that that will also strip non-ASCII letters; you may want to use a Unicode character class (e.g. "letter") instead.

分享给朋友:
您可能感兴趣的文章:
随机阅读: