# Regular Expression Problem

• (2 Pages)
• 1
• 2

## 15 Replies - 3102 Views - Last Post: 14 July 2007 - 10:58 PM

### #1 ajaymatrix

• D.I.C Regular

Reputation: 1
• Posts: 410
• Joined: 15-May 07

# Regular Expression Problem

Posted 11 July 2007 - 11:12 PM

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 ...
Is This A Good Question/Topic? 0

## Replies To: Regular Expression Problem

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Regular Expression Problem

Posted 11 July 2007 - 11:57 PM

This must be school/course work. Post the code you have written so far to try and solve this question.

### #3 ajaymatrix

• D.I.C Regular

Reputation: 1
• Posts: 410
• Joined: 15-May 07

## Re: Regular Expression Problem

Posted 12 July 2007 - 07:36 AM

/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...

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Regular Expression Problem

Posted 12 July 2007 - 10:16 AM

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:

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

```

### #5 ajaymatrix

• D.I.C Regular

Reputation: 1
• Posts: 410
• Joined: 15-May 07

## Re: Regular Expression Problem

Posted 12 July 2007 - 11:14 AM

hey..
that even i know if we know the string beforehand...

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Regular Expression Problem

Posted 12 July 2007 - 12:01 PM

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

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 July 2007 - 12:02 PM

### #7 ajaymatrix

• D.I.C Regular

Reputation: 1
• Posts: 410
• Joined: 15-May 07

## Re: Regular Expression Problem

Posted 12 July 2007 - 01:39 PM

no the test was not online...

maybe they didn't want the exact soln.. maybe they were looking for the approach...

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Regular Expression Problem

Posted 12 July 2007 - 02:25 PM

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?

### #9 ajaymatrix

• D.I.C Regular

Reputation: 1
• Posts: 410
• Joined: 15-May 07

## Re: Regular Expression Problem

Posted 12 July 2007 - 09:32 PM

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...

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Regular Expression Problem

Posted 13 July 2007 - 10:50 AM

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.

### #11 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: Regular Expression Problem

Posted 13 July 2007 - 11:27 AM

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
```/[ABCD]*BB[ABCD]*/
```

I apologize if I intrude but I have to try.

• g+ + -o drink whiskey.cpp

Reputation: 253
• Posts: 13,507
• Joined: 12-July 02

## Re: Regular Expression Problem

Posted 14 July 2007 - 01:05 PM

Kevin is correct - regular expressions are a matching/description tool - they cannot build all possible permutations of a string.

### #13 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: Regular Expression Problem

Posted 14 July 2007 - 01:33 PM

Hi I see now after I read it all carefully that my try was wrong.
I tried to solve this but I don't think it can be solved with just reg expr, as usual the more experienced ones are right.

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Regular Expression Problem

Posted 14 July 2007 - 02:24 PM

The easiest way to get the permutations is to use the List::Permutor module:

```use List::Permutor;
my \$perm = new List::Permutor qw/A B C D/;
while (my @set = \$perm->next) {
print "One order is @set.\n";
}
```

but of course there are never two B's in any of the permutations. We would have to add another B to the list but then they will all have two B's.

I think ajaymatrix might have misunderstood the original question or did not ask the same question as was on the test. Hard to say since the orignal question is not available for examination.

### #15 ajaymatrix

• D.I.C Regular

Reputation: 1
• Posts: 410
• Joined: 15-May 07

## Re: Regular Expression Problem

Posted 14 July 2007 - 09:13 PM

I was never sarcastic..
I really appreciate the help you gave me in understanding this question...