Welcome to Dream.In.Code
Become an Expert!

Join 149,507 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,327 people online right now. Registration is fast and FREE... Join Now!




Regular Expression Problem

2 Pages V  1 2 >  
Reply to this topicStart new topic

Regular Expression Problem

ajaymatrix
11 Jul, 2007 - 10:12 PM
Post #1

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 395



Thanked: 1 times
Dream Kudos: 100
My Contributions
Given a set of alphabets S= {A,B,C,D}

Write a Regular Expression for Determining all possible combinations where the letter B occurs twice..

eg :

ABCB : valid
BBCD : valid
ABBB : invalid
DCBA : invalid


I know the code if the 2 B's occur together which is rather simple...

Any one has some idea ...


User is offlineProfile CardPM
+Quote Post

KevinADC
RE: Regular Expression Problem
11 Jul, 2007 - 10:57 PM
Post #2

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
This must be school/course work. Post the code you have written so far to try and solve this question.
User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: Regular Expression Problem
12 Jul, 2007 - 06:36 AM
Post #3

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 395



Thanked: 1 times
Dream Kudos: 100
My Contributions
/BB/ is for 2 Bs occuring together..

but if the Bs are occuring at some other position then how to determine...
I am not that very good at Regex..

It was not school/course work.. this was the question asked in a popular web company's aptitude test...
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: Regular Expression Problem
12 Jul, 2007 - 09:16 AM
Post #4

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
That seems to have nothing to do with the question:


Write a Regular Expression for Determining all possible combinations where the letter B occurs twice..

Your regular expression is just checking if there are two consecutive B's in the string, it's not "determining all possible combinations". I don't think a regexp can do that. But if the strings are already constructed and all you need to do is see if there are two B's anywhere:

CODE
my $foo = 'BCBD';
if (2 == ($foo =~ tr/B/B/)) {
   print "Valid\n"
}
else {
   print "Invalid\n";
}    




User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: Regular Expression Problem
12 Jul, 2007 - 10:14 AM
Post #5

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 395



Thanked: 1 times
Dream Kudos: 100
My Contributions
hey..
that even i know if we know the string beforehand...

User is offlineProfile CardPM
+Quote Post

KevinADC
RE: Regular Expression Problem
12 Jul, 2007 - 11:01 AM
Post #6

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
first you have to figure out all the possible permutations of two B's within the character set: A,B,C,D, for example, assuming only the B can be repeated:

ABBB
ABBC
ABBD
ACBB
ADBB

and so on.

I don't think you would even use a regular expression for that so the original question makes little sense to me. Is this apptitude test online?

This post has been edited by KevinADC: 12 Jul, 2007 - 11:02 AM
User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: Regular Expression Problem
12 Jul, 2007 - 12:39 PM
Post #7

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 395



Thanked: 1 times
Dream Kudos: 100
My Contributions
no the test was not online...

maybe they didn't want the exact soln.. maybe they were looking for the approach...
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: Regular Expression Problem
12 Jul, 2007 - 01:25 PM
Post #8

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
well, that is the question you posted so I am not sure what further advice to give you. Post the actual question if you can, if not, I guess thats the end of this discussion?
User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: Regular Expression Problem
12 Jul, 2007 - 08:32 PM
Post #9

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 395



Thanked: 1 times
Dream Kudos: 100
My Contributions
that was a written test and the question posted above was what was asked..
Maybe instead of a complete solution they were expecting what was your approach...


so the question remained pretty ambiguous and I couldn't think of any solution..

That is why I thought it would be a better to have a discussion in this forum..

Any way thanks for your precious time Kevin...
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: Regular Expression Problem
13 Jul, 2007 - 09:50 AM
Post #10

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
I am not sure if you are being saracastic or not about my precious time. If you're not, you're welcome. If you are, well, sorry you feel that way, I was just trying to help you the best I know how.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Regular Expression Problem
13 Jul, 2007 - 10:27 AM
Post #11

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, I don't belong to this forum i.e. I know nothing about Perl or Python, but I wonder if this is the regular expression that you want
CODE
/[ABCD]*BB[ABCD]*/

I apologize if I intrude but I have to try.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Regular Expression Problem
14 Jul, 2007 - 12:05 PM
Post #12

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Kevin is correct - regular expressions are a matching/description tool - they cannot build all possible permutations of a string.
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month