Objective: To get all the non-alphanumeric characters even though they are not contiguous.
Setup: I have a textbox on an ASP.Net page that calls a C# code behind method on TextChanged. This event handler runs the textbox input against a Regex pattern.
Problem: I cannot create the proper Regex pattern that extracts all the non-alphanumeric characters.
This is the string input: string titleString = @"%2@#$%^&";
These are the C# Regex Patterns that I have tried:
string titlePattern = @"(\b[^A-Za-z0-9]+)"; results with @#$%^& (Note: if I use this input string %2@35%^&, then the above regex pattern will identify the @ sign, and then the %^&), but never the leading % sign).
string titlePattern = @"(\A[^A-Za-z0-9]+)"; results with %
string titlePattern = @"(\b\W[^A-Za-z0-9]+)"; results with @#$%^&
Side Note: I am also running this in a MS Visual Studio Console Application with a foreach loop in an effort to get all invalid characters into a collection and I also test the input and pattern using the web site: http://regexstorm.net/tester
char[] nonAlphaNumeric = inputString?.Where(c => !(char.IsLetter(c) || char.IsNumber(c))).ToArray();(\b[^A-Za-z0-9]+)not matching the leading%: Look at your regex. What do you believe\bstands for there in your Regex vs. what is the documentation saying about what\bdoes?