Friday 15 June 2012

c# - Remove non-word characters and leading numbers from string -



c# - Remove non-word characters and leading numbers from string -

i have text field feeds label through code. user types text field, automatically parse text, , strip out non-word characters text , place label.

private void text_keyup(object sender, keyeventargs e) { label.content = regex.replace(text.text, @"[^\w]", "").tolower(); }

however, have constraint label field, , can't figure out how it. current regex have makes label can have lowercase word characters. however, need create sure first character in label not number. have tried ^[^a-za-z][^\w], isn't working. got ideas?

the original problem says "replace {an english language letter} with.." , not "replace {a number} with.."; after update still problematic because considers 2 operations compound - replace non-a-z only if not followed word character.

compare rewriting so:

var filtered = text.text; filtered = regex.replace(filtered, @"\w+", ""); // trim non-word characters filtered = regex.replace(filtered, @"^\d+", ""); // remove number(s) @ start label.content = filtered.tolower();

do note that, default, in c#/.net escapes \w , \d (and opposites) unicode-aware. makes them different javascript / pcre / java / perl, etc., more english-centric.

c# regex

No comments:

Post a Comment