423,682 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,682 IT Pros & Developers. It's quick & easy.

Regular Expression: how match a whole word?

P: n/a
Regular Expression: how match a whole word?

Hi All,
First of all, Id express all my gratitude to all whom will help me about this topic.
I have a question about the Regular Expression Engine, the issue of mine is: Can I match an entire word using negative clauses?
In English: Ive a simple text as: man,men,mon,mun,car,cer,cir and Id match all the words which dont contain in the first position the letter m, in the second a and in the third n. In this way:

Text: man,men,mon,mun,car,cer,cir
Word To Search: all the words which dont contain the letters [m][a][n] in the co respective positions.
Pattern: [^m][^a][^n] (surely incorrect)

If I use as Pattern [m][a][n], Im able to find the word man, but how I could the reverse process?
Id like to retrieve all others words except this, is it possible?

Sorry, for this kind of simple question, I tried several times before creating this post, but all the searches of mine about this topic have been unsatisfied.
Oct 29 '10 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
a very simple way of defining the RegEx just excludes the letter from the list: [a-ln-z][b-z][a-mo-z]. [^m][^a][^n] is not principally wrong, though it also matches non-letters.
Oct 29 '10 #2

P: n/a
Hi Dormilich,

first of all thank you for your reply!
You're right I mean I could remove all the letters I'd remove, but just for better understand the Regular Expression Engine: how I'd search using negative cluases?

In case I use a pattern like: [^m][^a][^n], instead of searching for all the words which don't contain the pattern "man", the engine searches for all the words which don't contain the letter "m: at the first position, the letter "a" at the second position and the letter "n" at the third one.
The result using the precedent text example (man,men,mon,mun,car,cer,cir ) will be "an,en,on,un,ar,cer,ci".

Id like to retrieve all others words except this, is it possible?
Oct 30 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
The result using the precedent text example (“man,men,mon,mun,car,cer,cir” ) will be "an,en,on,un,ar,cer,ci".
no. given you use /[^m][^a][^n]/g on this string, you get: "an,", "en,", "on,", "un,", "ar,", "cer", ",ci". what you request is a 3 character match, thus your result is a couple of matching 3 characters strings.
Nov 1 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.