0

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?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78

1 Answers1

3

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.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • That worked, thanks. I didn't think to have the requirements on two lines instead of one. – Jason Axelrod Oct 07 '14 at 04:50
  • @JasonAxelrod I like regular expressions .. but only in small bites. This could be written as a single regular expression with the use of an alternation (`@"^\d+|\W+"`), I chose the above mostly to show a point. – user2864740 Oct 07 '14 at 04:50
  • Yeah. I use a TON of regex in my work... become very good at them. The issue for me was figuring out how C# works, not the regex. – Jason Axelrod Oct 07 '14 at 04:53