Regular expression for word counting

D

drasko

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?
 
G

Guest

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
}
 
J

Jesse Houwing

* 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
 
G

Guest

* drasko wrote, On 14-7-2007 11:57:


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...
 
D

drasko

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

Guest

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...
 
J

Jesse Houwing

* 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top