?
Solved

Counting Characters less whitespace, with Regular Expression

Posted on 2006-06-08
4
Medium Priority
?
281 Views
Last Modified: 2010-04-23
VB.NET 2003

I'm trying to count the words and characters in a text box. I picked up the CountWords function from a post by TheLearnedOne and it works great. I was trying to build off of it and make a function that would count all characters with the exception of any whiteSpace. The function I wrote, CountCharacters is not working. Any help in counting the characters without counting spaces would really be appreciated.

 Public Shared Function CountWords(ByVal inputText As String) As Integer
        Dim patternWords As String = "[\w]+"
        Dim regCountWords As New System.Text.RegularExpressions.Regex(patternWords)
        Return regCountWords.Matches(inputText).Count()
    End Function 'CountWords'


    Public Shared Function CountCharacters(ByVal inputText As String) As Integer
        Dim patternCharacters As String = "[^:Wh]"
        Dim regCountCharacters As New System.Text.RegularExpressions.Regex(patternCharacters)
        Return regCountCharacters.Matches(inputText).Count()
    End Function 'CountCharacters'
0
Comment
Question by:Gary Samuels
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:neilprice
ID: 16863013
Hi,

You can try just removing the spaces first then counting individual characters like this;

    Public Function CountCharacters(ByVal inputText As String) As Integer
        Dim tempText As String = inputText.Replace(" ", "")
        Dim patternCharacters As String = "[\w]"
        Dim regCountCharacters As New System.Text.RegularExpressions.Regex(patternCharacters)
        Return regCountCharacters.Matches(tempText).Count()
    End Function 'CountCharacters'

Hope this helps,
Neil
0
 

Author Comment

by:Gary Samuels
ID: 16863834
That works but I still have a problem. The CountCharacters function is not counting spaces now, but it also is not counting any puncuation.
Looking up Regular Expressions in help I see that:

[^...]   =   Matches any character not in the set of characters following the ^.
:Wh     =   Matches all types of whitespace, including publishing and ideographic spaces.

This is where I though that
Dim patternCharacters As String = "[^:Wh]"
would count any characters except whitespace.

Does anyone know the correct Regular Expressions syntax that will count all characters except whitespace?

0
 
LVL 6

Accepted Solution

by:
neilprice earned 1500 total points
ID: 16864218
Okay try this;

    Public Function CountCharacters(ByVal inputText As String) As Integer
        Dim patternCharacters As String = "[^:Wh ]" ' Added a space in here at the end before the closing square bracket
        Dim regCountCharacters As New System.Text.RegularExpressions.Regex(patternCharacters)
        Return regCountCharacters.Matches(inputText).Count()
    End Function 'CountCharacters'

Neil
0
 

Author Comment

by:Gary Samuels
ID: 16866816
I think we have it. I had to make a few changes. I realized that the "[^:Wh ]" was removing the "h" as well as the "whiteSpace". Then I realized I needed to remove the "newline" and "tab". I know there are other ways to do this but I am using this in real time as the operator types the counts are continously updated. That's why I wanted to go with the Regular Expressions because of the speed. Thanks for the help.


    Public Shared Function CountCharacters(ByVal inputText As String) As Integer
        Dim patternCharacters As String = "[^:W \n \t ]"   'whiteSpace, newline, and tabs removed from count
        Dim regCountCharacters As New System.Text.RegularExpressions.Regex(patternCharacters)
        Return regCountCharacters.Matches(inputText).Count()
    End Function 'CountCharacters'
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses
Course of the Month6 days, 4 hours left to enroll

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question