1

Possible Duplicate:
Regex - Only letters?

I try to filter out alphabetics ([a-z],[A-Z]) from text.

I tried "^\w$" but it filters alphanumeric (alpha and numbers).

What is the pattern to filter out alphabetic?

Thanks.

0

4 Answers 4

3

To remove all letters try this:

void Main()
{
    var str = "some junk456456%^&%*333";
    Console.WriteLine(Regex.Replace(str, "[a-zA-Z]", ""));
}
Sign up to request clarification or add additional context in comments.

3 Comments

String.Replace or Regex.Replace ?
Updated to be in the requested language and make sense.
Excelnt! thanks. could you please explain me what did you do?
2

For filtering out only English alphabets use:

[^a-zA-Z]+

For filtering out alphabets regardless of the language use:

[^\p{L}]+

If you want to reverse the effect remove the hat ^ right after the opening brackets.

If you want to find whole lines that match the pattern then enclose the above patterns within ^ and $ signs, otherwise you don't need them. Note that to make them effect for every line you'll need to create the Regex object with the multi-line option enabled.

5 Comments

Using Sublime Text 2 the first example works as described, but the 2nd example doesn't. Don't know whether this is a quirk of the regex engine in ST2 though.
Probably difference between regex engines. I have tested them with RegexHero which is a .NET based engine, and they work as expected.
I tested them in Notepad++ too, and they work fine there too.
+1 for the correct answer. For info: you don't need to put the Unicode property into a negated character class [^\p{L}], you can just write \P{L} with an uppercase P to get the negated version.
@stema Thanks. Learning something new everyday.
0

try this simple way:

var result = Regex.Replace(inputString, "[^a-zA-Z\s]", "");

explain:

+ Matches the previous element one or more times.

[^character_group] Negation: Matches any single character that is not in character_group.

\s Matches any white-space character.

Comments

-1

To filter multiple alpha characters use

^[a-zA-Z]+$

3 Comments

It test it out thanks, but isn't it filter in? I mean, it filter everything BUT this? (a-zA-Z)
@user1798362 You question implied that \w did what you wanted but for alphanumerics, so don't be surprised if you get answers just replacing "alphanumerics" with "alpha" without magically knowing what else might be wrong with your code.
Not sure I understand. Do you want to select everything except alpha characters?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.