World's most popular travel blog for travel bloggers.

[Solved]: regular expression in license plates

, , No Comments
Problem Detail: 

I'm trying to write a regular expression for some particular license plates. They consist of one up to three capital letters, a hyphen, one up to two capitol letters and one up to four numbers. The license plate should not exceed the 8 symbols.

Example: AA-AA123

What I have until now is the following:

because there has to be at least one letter before the hyphen, I have this expression for the first letter: $$ P = (A\cup B \cup...\cup Z) $$

For the second letter: $$ D = (A\cup B \cup...\cup Z\cup empty) $$ empty indicates the possibility that there could not be a letter.

For the numbers is basically almost the same procedure: $$ E = (1\cup 2 \cup...\cup 9) $$ $$ F = (1\cup 2 \cup...\cup 9 \cup empty) $$

now for my explicit expression I think the answer would be: $$ K= PDD^* - PDEF^*F^* $$

but I'm not quite sure. I think this means I have 8 symbols, at least one letter before the hyphen but I could have up to three, the hyphen (that is a must), at least one letter after the hyphen but up to two and at least one number but I could have up to 4.

Something tells me I have something wrong here, I'm still new in the regular expressions world and I appreciate any help I could get in advance.

Asked By : nubz0r

Answered By : Artemis

As David Richerby pointed out $D^*$ will accept an arbitrary number of letters (including none). If I'm not mistaken you need to take all possible options to achieve not having more than 8 characters. $$ K = ( PDD-PDEF ) \cup (PDD-PEFF) \cup (PD-PDEFF) \cup (PD-PEFFF) \cup (P-PDEFFF) $$ This assumes the hyphen is one of the 8 letters and thus does not allow more than 8 letters. Less letters are allowed because of your definitions of $F$ and $D$ including the empty word $\varepsilon$.

EDIT: added missing cases from commenter.

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/24293

3.2K people like this

 Download Related Notes/Documents

0 comments:

Post a Comment

Let us know your responses and feedback