Slapshot
Slapshot

Reputation: 45

How can I extract all non-alphanumeric characters from an input string using Regular Expressions?

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

Upvotes: 1

Views: 909

Answers (1)

Display name
Display name

Reputation: 1552

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}");
        }
    }
}

Upvotes: 3

Related Questions