0

I'm actually trying to match some order numbers in a string.

The string could look like this:

SDSFwsfcwqrewrPL0000018604ergerzergdsfa

or

FwsfcwqrewrAT0000018604ergerzergdsfaD

and I need to match the "PL0000018604" or "AT0000018604".

Actually I'm using something like that and it works:

.?(AT[0-9]{10})|(BE[0-9]{10})|(FR[0-9]{10})|(IT[0-9]{10})

But the more order prefixes we get, the longer the expression will be. It is always 2 uppercase chars followed by 10 digits and I want to specify the different uppercase chars.

Is there any shorter version?

Thanks for your help :)

DeeKaay
  • 3
  • 1

1 Answers1

2

If the prefixes must be specific, there's not much of a way to make the pattern shorter. Though, you can collect all the prefixes at the front of the expression so you only have to have the numeric part once.

for example:

(AT|BE|FR|IT)[0-9]{10}

Depending on how you call it, if you need the whole expression to be captured as a group (versus simply matching, which is what the question asked about), you can add parenthesis around the whole expression. That doesn't change what is matched, but it will change what is returned by whatever function uses the expression.

((AT|BE|FR|IT)[0-9]{10})

And, of course, if you just want the number part to also be captured as a separate group, you can add more parenthesis

((AT|BE|FR|IT)([0-9]{10}))
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks, I already tried that and the match will be "PL" or "AT" – DeeKaay Sep 28 '15 at 13:32
  • @DeeKaay: I don't understand your comment. If you want to match PL, add it to the list `(AT|PL|...)` – Bryan Oakley Sep 28 '15 at 13:33
  • Your expression will not match "AT0000018604" it will only match "AT" – DeeKaay Sep 28 '15 at 13:35
  • @DeeKaay: no, it will match AT followed by 10 digits. The pattern says "match one of AT, BE, FR or IT, followed by exactly 10 characters in the range 0-9" – Bryan Oakley Sep 28 '15 at 13:37
  • well it will match only AT because of mathing group of 1 if used in .net framework. If used whole match it is at index 0 – Igoris Azanovas Sep 28 '15 at 13:38
  • Yes it will but the return value will only be AT but I need AT and the 10 digits. not the complete line :) – DeeKaay Sep 28 '15 at 13:39
  • `(AT|BE|FR|IT)([0-9]{10})` Try this and look at index + 1, which should have the other group in brackets – Igoris Azanovas Sep 28 '15 at 13:39
  • @IgorisAzanovas: matching group zero will match the AT part. The question didn't mention anything about matching groups, and mentioned nothing about .net. It only asked about a pattern that matches a particular type of string. If you need a matching group, just add one. I've updated my answer to include that. – Bryan Oakley Sep 28 '15 at 13:39
  • I'm not using .net, it's for php. Thanks I will try it :) – DeeKaay Sep 28 '15 at 13:53