Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Regular expression for word counting

Reply
Thread Tools

Regular expression for word counting

 
 
drasko
Guest
Posts: n/a
 
      07-14-2007
Hi, I have RegularExpressionValidator control and I wish to set it up
for limiting input text in textarea to 50 words. Users can enter text
they like, using all ".",",","!","?","@" and other symbols. All that
matters is that there can be maximum 49 whitespace characters. How can
write regular expression for this one?

 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      07-14-2007
On Jul 14, 11:57 am, drasko <(E-Mail Removed)> wrote:
> Hi, I have RegularExpressionValidator control and I wish to set it up
> for limiting input text in textarea to 50 words. Users can enter text
> they like, using all ".",",","!","?","@" and other symbols. All that
> matters is that there can be maximum 49 whitespace characters. How can
> write regular expression for this one?


Regex regex = new Regex(@"\w+");
MatchCollection matches = regex.Matches(inputtext);

if(matches.Count > 50)
{
// Error
}

 
Reply With Quote
 
 
 
 
Jesse Houwing
Guest
Posts: n/a
 
      07-14-2007
* drasko wrote, On 14-7-2007 11:57:
> Hi, I have RegularExpressionValidator control and I wish to set it up
> for limiting input text in textarea to 50 words. Users can enter text
> they like, using all ".",",","!","?","@" and other symbols. All that
> matters is that there can be maximum 49 whitespace characters. How can
> write regular expression for this one?
>


This one comes close:

^(\S+\s){0,49}\S+$

It does require the string not to begin with whitepaces, nor end with
it. So you might want to put a javascript onBlur on the textbox to trim
the contents.

It reads as follows:

^ ensure we check the string from the beginning
\S+ find a 'word' (something with no whitespaces)
\s followed by a whitespace
repeat this 0 up to 49 times

followed by \S+ which is another 'word'.
$ ensure we check the string all the way to the end.

Jesse
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      07-14-2007
On Jul 14, 2:18 pm, Jesse Houwing <(E-Mail Removed)>
wrote:
> * drasko wrote, On 14-7-2007 11:57:
>
> > Hi, I have RegularExpressionValidator control and I wish to set it up
> > for limiting input text in textarea to 50 words. Users can enter text
> > they like, using all ".",",","!","?","@" and other symbols. All that
> > matters is that there can be maximum 49 whitespace characters. How can
> > write regular expression for this one?

>
> This one comes close:
>
> ^(\S+\s){0,49}\S+$
>
> It does require the string not to begin with whitepaces, nor end with
> it. So you might want to put a javascript onBlur on the textbox to trim
> the contents.
>
> It reads as follows:
>
> ^ ensure we check the string from the beginning
> \S+ find a 'word' (something with no whitespaces)
> \s followed by a whitespace
> repeat this 0 up to 49 times
>
> followed by \S+ which is another 'word'.
> $ ensure we check the string all the way to the end.
>
> Jesse


Ah, Jesse you're right, I forgot about RegularExpressionValidator...

 
Reply With Quote
 
drasko
Guest
Posts: n/a
 
      07-14-2007


Guys, thank you very much. Does your reg ex include , . ! ? or similar
at the end, or somewhere in the middle?

 
Reply With Quote
 
drasko
Guest
Posts: n/a
 
      07-14-2007
Guys, thank you so much!!!

 
Reply With Quote
 
drasko
Guest
Posts: n/a
 
      07-14-2007
Doesn't work...

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      07-14-2007
On Jul 14, 10:19 pm, drasko <(E-Mail Removed)> wrote:
> Doesn't work...


Use this one

<asp:TextBox ID="TextBox1" runat="server" Height="184px"
TextMode="MultiLine" Width="224px"></asp:TextBox>

<asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="RegularExpressionValidator" ValidationExpression="(((^
\s*)*\S+\s+)|(\S+)){1,50}"
Display="Dynamic"></asp:RegularExpressionValidator>

<asp:Button ID="Button1" runat="server" CausesValidation="true"
OnClick="Button1_Click1" Text="Button" />

This is used to validate a word count, not a whitespace characters.
So, if you need to be sure that there is no space at the end, make a
trim at the code-behind...

 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      07-15-2007
* drasko wrote, On 14-7-2007 22:19:
> Doesn't work...
>


The regex I sent does not allow for multiple whitespace characters right
after eachother.

Altering it to:

^(\S+\s+){0,49}\S+$

should work, but that counts up to 49 gaps, not up to 49 exact
whitespace characters.

If you want to be more exact I suggest you sue a custom validator and
write the javascript and server side logic yourself using the normal
String functions.

Jesse
 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      07-15-2007
* drasko wrote, On 14-7-2007 22:19:
> Doesn't work...
>


Can you provide us with the texts you've tried? That way I can verify
what's going wrong.

Jesse
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments