I have the following regex that I use to validate North American postal codes:
(?:(\d{5})(?:-\d{4})?)|(?:([a-zA-Z]\d[a-zA-Z]) ?(\d[a-zA-Z]\d))
FYI, I understand that it could be more exact, in regards to verifying certain characters in certain positions.
What I'd like to do is use this same validation constant to also extract the postal code in the format:
00000
or
a0a0a0
The regex above comes close; if I concatenate all of the capturing groups (except for the root), I get the result I seek. For example, a US code will capture in group 1, or a Canadian code will capture in 2 + 3.
Is there a better way to do this? Or maybe that is assumed it would be a feature of a regex library, to concatenate all subgroups. (Incidentally, this is C# .NET)