473,465 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regular expression for word counting

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?

Jul 14 '07 #1
9 3383
On Jul 14, 11:57 am, drasko <draskosa...@gmail.comwrote:
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
}

Jul 14 '07 #2
* 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
Jul 14 '07 #3
On Jul 14, 2:18 pm, Jesse Houwing <jesse.houw...@nospam-sogeti.nl>
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...

Jul 14 '07 #4


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

Jul 14 '07 #5
Guys, thank you so much!!!

Jul 14 '07 #6
Doesn't work... :(

Jul 14 '07 #7
On Jul 14, 10:19 pm, drasko <draskosa...@gmail.comwrote:
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...

Jul 14 '07 #8
* 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
Jul 15 '07 #9
* 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
Jul 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own....
3
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
3
by: Ryan Taylor | last post by:
Hello. I am trying to create a regular expression that will let me know if a string has the following criteria. Order does not matter in the string, but when building a regular expression it...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.