0

Hello I am attempting to read vat numbers in regex with the country code in front:

GB 009 0009 09

that they could be formatted as so

GB009000909
GB 009000909
GB 0 09 0009 09

My current expression is: (GB\d{9}\b|GB\s{1}\d{9}\b)

How do i go about keeping it simple and ignoring spaces?

Todd Owen
  • 97
  • 1
  • 9
  • may be useful https://stackoverflow.com/questions/4590298/how-to-ignore-whitespace-in-a-regular-expression-subject-string – Aidenhjj Jan 22 '20 at 14:39

2 Answers2

3

You can use GB(\s?\d){9}

GB matches the characters GB literally (case sensitive)

1st Capturing Group (\s?\d){9}

{9} Quantifier — Matches exactly 9 times

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data

\s? matches any whitespace character (equal to [\r\n\t\f\v ])

? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)

\d matches a digit (equal to [0-9])

test it yourself

Cid
  • 14,968
  • 4
  • 30
  • 45
  • 1
    @ToddOwen thanks, but I'm not a legend about RegEx. [Wiktor Stribiżew](https://stackoverflow.com/users/3832970/wiktor-stribi%c5%bcew) is a legend ! – Cid Jan 22 '20 at 14:48
  • He just closed this for one that is relevant to c# not much help to me but thanks anyway – Todd Owen Jan 22 '20 at 15:01
1
  • The pattern [ ]* will match any number of spaces (including 0).
  • The pattern ([0-9]+) will match 1 or more digits.
  • The pattern ([0-9]*) will any number of digits(including 0).

I think something like this could match everything for you:

GB[ ]*([0-9]+)[ ]*([0-9]*)[ ]*([0-9]*)[ ]*([0-9]*)

Match GB every time. Then 0 or more spaces, then at least 1 number, then spaces then numbers up to 4 times if 0 or more appear.

Let me know if more explanation is needed!

Branson Smith
  • 462
  • 2
  • 13