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

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

##
**Replies To:** Regular Expression Problem

### #2

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

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

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

### #4

## 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:

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

## Re: Regular Expression Problem

Posted 12 July 2007 - 11:14 AM

hey..

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

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

### #6

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

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?

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

### #7

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

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

### #8

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

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

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

### #10

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

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

I apologize if I intrude but I have to try.

/[ABCD]*BB[ABCD]*/

I apologize if I intrude but I have to try.

### #12

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

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

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.

### #14

## 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:

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.

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

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

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