tags:

views:

438

answers:

4

Hi, Vonc,

The regular expression which you gave: ^(?:\b\w+\b[\s\r\n]*){1,250}$ to limit 250 words over multiple lines works if it doesn't have any special characters... what should i do if i need to search for number of words which also consists special characters... Some thing like this an example: --> Hi! i need help with regular expression, please help me. <--

A: 

You can use the {a,b} quantifiers on any expression, like so:

.{1,256}
[\d\w_?]{1,567}
(0x)?[0-9A-F]{1,}

So, in your case, you could use:

^(?:\b\w+\b[_!?\s\r\n]*){1,250}$

Where the _!? can be any special characters.

Lucas Jones
A: 

Hi Person-b,

I tried that and it works fine.. Thank You... Only thing is i am unable to pass ampersand "&" and Single quote "'"

I am bascially coding in VB.NET

Do you have any other suggestion. Thanks a ton for your reply.

Harish

Comment on answers, not comment with an answer.
Dykam
A: 

The simplest approach is to group the word characters, and limit those groups to a specific range (1-250):

^\W*(\w+(\W+|$)){1,250}$
Justin Ludwig
+1  A: 

I am not familiar with C# so I will describe the regex.

Method 1:

You are basically looking for this:

(\b[^\s]+\b){1,250}

In java:

\s is any whitespace character.

[^\s]+ is a sequence of non-whitespace characters.

\b is a word boundary.

You can translate the regex to C#.

Method 2:

Tokenize the input text into whitespace delimited words. In java, this is done by:

String[] tokens = inputString.split("\\s+");

where the regex is \s+

Now you can count the length of the array and implement your logic to reject the words beyond 250.

Method 3:

Define a pattern to capture whitespace as a 'capturing group'.

(\s+)

Now you can do a count the number of matches in your pattern matcher using a while loop. This is essentially kinda same as Method 2 but without involving the creation of the array of tokens.

hashable