Botond
Botond

Reputation: 95

How to make a regex pattern that fits

I'm trying to learn regex and came across a question I cannot solve. The pattern looks like this:

A1234567 4DFDGB

B1234567 1234DFDRR

C1234567 12DBFDG

The parts I want are the highlited ones. 1 letter followed by 7 numbers then a space and less than 5 numbers.

Thank you in advance!

Upvotes: 0

Views: 79

Answers (2)

hypertensy
hypertensy

Reputation: 235

If I understood correctly, then this is what you need

[A-Z]{1}\d{7}\s\d{1,5}

View details:

  • [A-Z]{1} - 1 letter from the A-Z range
  • \d{7} - 7 digits
  • \s - space
  • \d{1,5} - numbers range from 1 to 5

Upvotes: 4

Tom
Tom

Reputation: 725

(\w{1}([\d ]+))

This Will Work

\w{1} - Will match 1 Letter

([\d ]+)) - Will match any digits and spaces or ([\d ]{1,6}) Which will only match up to 5

Example here

Upvotes: 0

Related Questions