Calvin
Calvin

Reputation: 81

Regex Counting Characters in a text field

I am trying to count the characters in a text field. I found how to count all the characters.

string st = TextBox1.Text;
this.TextBox2.Text = Regex.Matches(st, ".|").Count.ToString();

But I need to create 2 more separate counts, Any Caps, numbers, - or # up to (not including) @

eg. LA-FG4-DETF-DJJJTHD-S@T-JHF-F1-F2 the count would be 21

and the other one I need to count from the @ (including) , Any Caps, numbers, - or # to the end of the text field.

eg. LA-FG4-DETF-DJJJTHD-S@T-JHF-F1-F2 the count would be 12

Any help would be appreciated.

Upvotes: 1

Views: 5185

Answers (1)

Ergwun
Ergwun

Reputation: 12978

        string input = "LA-FG4-DETF-DJJJTHD-S@T-JHF-F1-F2";
        int atIndex = input.IndexOf('@');
        int count1 = Regex.Matches(input.Substring(0, atIndex), "[0-9A-Z#-]").Count;
        int count2 = Regex.Matches(input.Substring(atIndex, input.Length - atIndex), "[0-9A-Z#@-]").Count;

Upvotes: 1

Related Questions