How can I make sure the whole word is matched?
For example say I want to test for ONLY letters [abcdef]?
If i use /[abcdef]/ both "aaaaaaabbbb" and "aaaaaaakkkkk" will match. I want to make sure EVERY letter matches the pattern I give.
Can this be done?
P.S. I do not know the length of the word ahead of time so I can not use [abcdef]{x} where x is the length of the word
Simple question!
Moderator: General Moderators
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Simple question!
The regex '/[abcdef]/' will match a single letter anywhere in the regex, that's why "aaaaaaakkkkk" matches.Brodster wrote:How can I make sure the whole word is matched?
For example say I want to test for ONLY letters [abcdef]?
If i use /[abcdef]/ both "aaaaaaabbbb" and "aaaaaaakkkkk" will match. I want to make sure EVERY letter matches the pattern I give.
Can this be done?
P.S. I do not know the length of the word ahead of time so I can not use [abcdef]{x} where x is the length of the word
You will have to do '/[abcdef]+/', where the + means one or more. But if you try that, still "aaaaaaakkkkk" matches: the "aaaaaaa" now is the match.
So, you will have to "anchor" the beginning (^) and ending ($) of the string with that last pattern to make sure all characters in the string match it:
Code: Select all
'/^[abcdef]+$/'Re: Simple question!
write a regular expression for the only strings that are not generated over {a,b} by the expression (a+ b)* a (a+b)*
reasoning please
reasoning please
Re: Simple question!
Write a regular expression for strings that contain atleast one a and one b. show that its capable of generating the strings :
(i) aabbbabaa
(ii) bbbbaaa
(i) aabbbabaa
(ii) bbbbaaa
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Simple question!
As I suggested two times now: create your own thread instead of hijacking other people's thread.piyushj wrote:Write a regular expression for strings that contain atleast one a and one b. show that its capable of generating the strings :
(i) aabbbabaa
(ii) bbbbaaa