Simple question!

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Brodster
Forum Newbie
Posts: 1
Joined: Tue Jan 06, 2009 6:08 pm

Simple question!

Post by Brodster »

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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Simple question!

Post by prometheuzz »

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
The regex '/[abcdef]/' will match a single letter anywhere in the regex, that's why "aaaaaaakkkkk" matches.
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]+$/'
piyushj
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2009 10:40 am

Re: Simple question!

Post by piyushj »

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
piyushj
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2009 10:40 am

Re: Simple question!

Post by piyushj »

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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Simple question!

Post by prometheuzz »

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
As I suggested two times now: create your own thread instead of hijacking other people's thread.
Post Reply