Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  88] [ 6]  / answers: 1 / hits: 24362  / 10 Years ago, mon, february 2, 2015, 12:00:00

To validate only word simplest regex would be (I think)



/^w+$/


I want to exclude digits and _ from this (as it accept aa10aaand aa_aa now, I want to reject them)



I think it can be gained by



 /^[a-zA-z]+$/


which means I have to take a different approach other than the previous one.



but what if I want to exclude any character from this range
suppose I will not allow k,K,p,P or more.



Is there a way to add an excluding list in the range without changing the range.?


More From » regex

 Answers
11

To exclude k or p from [a-zA-Z] you need to use a negative lookahead assertion.



(?![kpKP])[a-zA-Z]+


Use anchors if necessary.



^(?:(?![kpKP])[a-zA-Z])+$


It checks for not of k or p before matching each character.



OR



^(?!.*[kpKP])[a-zA-Z]+$


It just excludes the lines which contains k or p and matches only those lines which contains only alphabets other than k or p.



DEMO


[#67988] Friday, January 30, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lewis

Total Points: 739
Total Questions: 100
Total Answers: 94

Location: Nepal
Member since Mon, Jan 4, 2021
3 Years ago
lewis questions
Tue, May 11, 21, 00:00, 3 Years ago
Fri, Jul 10, 20, 00:00, 4 Years ago
Wed, May 27, 20, 00:00, 4 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
;