1

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

6
  • 1
    char[] nonAlphaNumeric = inputString?.Where(c => !(char.IsLetter(c) || char.IsNumber(c))).ToArray(); Commented Mar 22, 2019 at 17:05
  • 2
    Regarding your (\b[^A-Za-z0-9]+) not matching the leading %: Look at your regex. What do you believe \b stands for there in your Regex vs. what is the documentation saying about what \b does? Commented Mar 22, 2019 at 17:18
  • Possible duplicate of Regex Expressions for all non alphanumeric symbols Commented Mar 22, 2019 at 18:03
  • Thanks to all for your responses. I am an ASP.Net/C# novice, and have never really used Regular Expressions, so I thought I would create the Character Class within the square brackets, and then apply NOT logic to the class, thereby attempting to get the remaining characters. I will read through the replies and attempt to employ and learn from the suggestions. Thank You. Commented Mar 23, 2019 at 14:32
  • Elgonzo: Yes, I realize the \b Anchor specifies the algorithm to begin searching at the word boundary. I was anticipating it would begin the search of the Character Class and reveal all the characters that are not contained within the Character Class. I realize that a % sign may not be considered a word boundary. Also, I've tried different combinations such as \A (The match must occur at the beginning of the string only) and \G (The match must start at the position where the previous match ended.) Commented Mar 23, 2019 at 16:01

1 Answer 1

3

Use the replace method with your selection string.

EDIT: After a closer reading I see that you wanted the opposite string. Here's both.

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string Source = @"H)*e/.?l\l{}*o ][W!~`@#""or^-_=+ld!";
            string Trash = @"[^a-zA-Z0-9]";
            string InvertedTrash = @"[a-zA-Z0-9]";

            Output(Source, Trash);
            Console.WriteLine($"{System.Environment.NewLine}Opposite Day!{System.Environment.NewLine}");
            Output(Source, InvertedTrash);
            Console.ReadKey();
        }
        static string TakeOutTheTrash(string Source, string Trash)
        {
            return (new Regex(Trash)).Replace(Source, string.Empty);
        }
        static void Output(string Source, string Trash)
        {
            string Sanitized = TakeOutTheTrash(Source, Trash);
            Console.WriteLine($"Started with: {Source}");
            Console.WriteLine($"Ended with: {Sanitized}");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.