Jason Axelrod
Jason Axelrod

Reputation: 7805

Remove non-word characters and leading numbers from string

I have a text field which feeds into a label through code. As the user types text into the field, it will automatically parse the text, and strip out non-word characters from the text and place it into the label.

private void text_KeyUp(object sender, KeyEventArgs e)
{
    label.Content = Regex.Replace(text.Text, @"[^\w]", "").ToLower();
}

However, I have another constraint to this label field, and I can't figure out how to do it. The current RegEx I have makes it so the label can only have lowercase word characters. However, I also need to make sure the first character in the label is not a number. I have tried ^[^A-Za-z][^\w], but that isn't working. Anyone got any ideas?

Upvotes: 0

Views: 1994

Answers (1)

user2864740
user2864740

Reputation: 61975

The original problem says "replace {an English letter} with.." and not "replace {a number} with.."; after the update it is still problematic because it considers the two operations to be compound - it will replace the non-A-Z only if it is not followed by a word character.

Compare with rewriting it as so:

var filtered = text.Text;
filtered = Regex.Replace(filtered, @"\W+", "");   // trim non-word characters
filtered = Regex.Replace(filtered, @"^\d+", "");  // remove number(s) at start

label.Content = filtered.ToLower();

Do note that, by default, in C#/.NET the escapes like \w and \d (and their opposites) are Unicode-aware. This makes them different from JavaScript / PCRE / Java / perl, etc., which are more English-centric.

Upvotes: 3

Related Questions