473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

regular expressions, word matching

Hi,

I want to count words in a string which are NOT with length 10. For me
word is a sequence of symbols different from ' '. So I want to match
these words. I think that the pattern has to be something like that "[^
]{10}|[ ]" but with not somewhere.
How to set to match not this pattern? Or other ideas?

Thanks.

Nov 17 '05 #1
1 1903
Use the following regex : [^ ]+(?<!\b[^ ]{10})\b
Explanation :
[^ ]+ means that you are looking for a sequence of non space
character
(?<! means that you are backtracking the previous sequence and you
want it NOT to match the following condition
\b this is to says that you sequence must start with a word
delimiter (here it is space, start of line, ....)
[^ ]{10} followed by 10 non space characters
) is the end of the backtracking
\b to be sure your word is complete and to avoid end of line
problems

Here an example to get the number of words wich are NOT with length 10 :
string mySentence="this is a sample sentence containing 2 words with 10
characters and 15 words with other size";
MessageBox.Show(Regex.Matches(mySentence,@"[^ ]+(?<!\b[^ ]{10})\b").Count+"
words without 10 characters length");

Here an example to get the words wich are NOT with length 10 :
string mySentence="this is a sample sentence containing 2 words with 10
characters and 15 words with other size";
Regex regex=new Regex(@"[^ ]+(?<!\b[^ ]{10})\b");
foreach(Match m in regex.Matches(mySentence)) MessageBox.Show(m.Value);

Hope it helps,

Ludovic SOEUR.
"Iwan Petrow" <xx****@abv.bg> a écrit dans le message de
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I want to count words in a string which are NOT with length 10. For me
word is a sequence of symbols different from ' '. So I want to match
these words. I think that the pattern has to be something like that "[^
]{10}|[ ]" but with not somewhere.
How to set to match not this pattern? Or other ideas?

Thanks.

Nov 17 '05 #2

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

Similar topics

1
by: Steve Zatz | last post by:
Is '@' a special character in regular expressions? I am asking because I don't understand the following: >>> import re >>> s = ' @' >>> re.sub(r'\b@','*',s) ' @' >>> s = ' a' >>>...
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...
1
by: pawel | last post by:
I have made some comparision C# to Java RegularExpression. The problem was to find out if the rule match some text. Matching were done for precompiled regular expressions, in 100000 iterations...
9
by: Schorschi | last post by:
Not having used regular expressions much, I need some help. Given a string... "This\0Guy\0Needs\0Some\0Help\0\0\0\0\0" Need result as array of strings... "This","Guy", "Needs", "Some", "Help" ...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
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...
6
by: John Salerno | last post by:
Ok, this might look familiar. I'd like to use regular expressions to change this line: self.source += '<p>' + paragraph + '</p>\n\n' to read: self.source += '<p>%s</p>\n\n' % paragraph ...
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...
12
by: =?Utf-8?B?SlA=?= | last post by:
I am a newbie to regular expressions and want to extract a number from the end of a string. The string would have these formats: image/4567 image/45678 image/456789 I would also want to...
5
Iasthaai
by: Iasthaai | last post by:
Hello everyone, I'm new to Python and also new to this forum and I have a question regarding regular expressions, so here goes: I am attempting to read in a text and do some pattern matching...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.