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? Something like this an example:
--> Hi! i need help with regular expression, please help me. <--
最简单的方法是对单词字符进行分组,并将这些分组限制在特定范围内(1-250):
^\W*(\w+(\W+|$)){1,250}$
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.
A bit late to answer but none of the solutions here worked:
^([a-zA-Z0-9]+[^a-zA-Z0-9]*){1,8}$
where {1,8} defines how many wordt you want
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.