Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count sets of specific characters in a string

Basically, I want to count sets of specific characters in a string. In other words I need to count all Letters and Numbers, and nothing else. But I cant seem to find the right (regex) syntax. Here's what i have ...

public double AlphaNumericCount(string s)
{
    double count = Regex.Matches(s, "[A-Z].[a-z].[0-9]").Count;
        return count;
}

I've been looking around, but cant seem to find anything that allows more than one set of characters. Again, I'm not sure on the syntax maybe it should be "[A-Z]/[a-z]/[0-9]" or something. Anywho, go easy on me - its my first day using Regex.

Thanks.

like image 982
Sizza Avatar asked Dec 19 '12 21:12

Sizza


1 Answers

Regular Expression Cheat Sheet

Expresso Regular Expression tool

[A-Z].[a-z].[0-9] will match any capital letter ([A-Z]), followed by any character (.), followed by any lower case letter ([a-z]), followed by any character (.), followed by any number ([0-9]).

What you want to match on any letter or number is [A-Za-z0-9].

like image 168
Bobson Avatar answered Sep 30 '22 08:09

Bobson