I am developing an application that takes text data sent from a QR code scan and performs a lookup from the CDC crosswalk tables. Here is a sample:
0110358160825436172302061095EJ7
The string data posted above contains the NDC number, vaccine lot number, and expiration date. However, I don't know how to parse out those fields.
How can I do this?
CodePudding user response:
This is PowerShell code that does the job. Below is what should work in C# code.
$QRScan = '0110358160825436172302061095EJ7';
$GS1AppID1 = $QRScan.Substring(0,2);
$GTIN = $QRScan.Substring(2,14);
$GS1AppID2 = $QRScan.Substring(16,2);
$YYMMDD = $QRScan.Substring(18,6);
$GS1AppID3 = $QRScan.Substring(24,2);
$LotNumber = $QRScan.Substring(26);
Write-Host "GS1AppID1: $GS1AppID1"
Write-Host "GTIN: $GTIN"
Write-Host "GS1AppID2: $GS1AppID2"
Write-Host "YYMMDD: $YYMMDD"
Write-Host "GS1AppID3: $GS1AppID3"
Write-Host "LotNumber: $LotNumber"
This is the output:
GS1AppID1: 01
GTIN: 10358160825436
GS1AppID2: 17
YYMMDD: 230206
GS1AppID3: 10
LotNumber: 95EJ7
I haven't tested in C#, but it should be very close to what you need.
string QRScan = "0110358160825436172302061095EJ7";
string GS1AppID1 = QRScan.Substring(0,2);
string GTIN = QRScan.Substring(2,14);
string GS1AppID2 = QRScan.Substring(16,2);
string YYMMDD = QRScan.Substring(18,6);
string GS1AppID3 = QRScan.Substring(24,2);
string LotNumber = QRScan.Substring(26);
NOTE: Substring can throw errors, so keep that in mind as you use it in your code.
Regex Versions
While Substring does the job, the question was tagged regex.
Regex C# console version (First time doing regex named groups in C# - Is there a simpler way?):
static void Main() {
string RE = @"^(?<GS1AppID1>\d{2})(?<GTIN>\d{14})(?<GS1AppID2>\d{2})(?<YYMMDD>\d{6})(?<GS1AppID3>\d{2})(?<LotNumber>\w )$";
var Parser = new Regex(RE);
string QRScan = "0110358160825436172302061095EJ7";
Match Matches = Parser.Match(QRScan);
if(Matches.Success) {
Console.WriteLine("GS1AppID1: {0}", Matches.Groups["GS1AppID1"].Value);
Console.WriteLine("GTIN: {0}", Matches.Groups["GTIN"].Value);
Console.WriteLine("GS1AppID2: {0}", Matches.Groups["GS1AppID2"].Value);
Console.WriteLine("YYMMDD: {0}", Matches.Groups["YYMMDD"].Value);
Console.WriteLine("GS1AppID3: {0}", Matches.Groups["GS1AppID3"].Value);
Console.WriteLine("LotNumber: {0}", Matches.Groups["LotNumber"].Value);
}
}
Regex PowerShell (Just for comparison.):
$RE = '^(?<GS1AppID1>\d{2})(?<GTIN>\d{14})(?<GS1AppID2>\d{2})(?<YYMMDD>\d{6})(?<GS1AppID3>\d{2})(?<LotNumber>\w )$';
$QRScan = '0110358160825436172302061095EJ7';
if($QRScan -match $RE) {
Write-Host "GS1AppID1: $($Matches.GS1AppID1)"
Write-Host "GTIN: $($Matches.GTIN)"
Write-Host "GS1AppID2: $($Matches.GS1AppID2)"
Write-Host "YYMMDD: $($Matches.YYMMDD)"
Write-Host "GS1AppID3: $($Matches.GS1AppID3)"
Write-Host "LotNumber: $($Matches.LotNumber)"
}