Reputation: 530
I am working on an Android app for Handheld Scan devices that should scan different Types of QR Codes; these QR Codes can contain different variations of digits, letters, dots and plus Signs; as I am not an expert for Regular Expressions, any hints or help would be very much appreciated.
The Regular Expression(s) should match the occurences of the following digits, letters, dots and plus signs:
1256+70
1235.B+70
1256+70+DB
1235.B+70+DB
1256+70+DB2020-123
1235.B+70+DB2020-123
1256+0+DB2020-123
1235.B+0+DB2020-123
The number range of the first four digits can be [100-99999].[A-Z]
I came up with the following Regular Expressions
[0-9]{4}
[0-9]{3,6}$.?[A-Z]?+[0-9]+DB[0-9]{4}-[0-9]]
[0-9]{3,6}$.?[A-Z]?+[0-9]
[0-9][0-9][0-9][0-9].[0-9][0-9].+DB
\b\d{3,6}\b
[0-9]{3,6}$.?[A-Z]?+[0-9]+DB[0-9]{4}-[0-9]]
[0-9]{3,6}$.?[A-Z]?+[0-9]
[0-9]{3,6}$.?[A-Z]?+[0-9]
but they are not covering all of the possible combinations and missing out a lot of options - hence any help or hints would be very much appreciated - thanks in advance!
Upvotes: 0
Views: 89
Reputation: 163362
Looking at the example data, one option could be using 1 regex with specific and optional parts:
^[1-9]\d{2,4}(?:\.[A-Z])?\+\d+(?:\+DB(?:\d{4}-\d{3})?)?$
^
Start of string[1-9]\d{2,4}
Match from 100 till 99999(?:\.[A-Z])?
Optionally match .
and a char A-Z\+\d+
match +
and 1+ digits (or use \d{1,2}
to match 1 or 2 digits)(?:
Non cpature group
\+DB(?:\d{4}-\d{3})?
match +
and DB and optionally match 4 digits -
and 3 digits)?
Close non capture group and make it optional$
End of stringIn Java
String regex = "^[1-9]\\d{2,4}(?:\\.[A-Z])?\\+\\d+(?:\\+DB(?:\\d{4}-\\d{3})?)?$";
Upvotes: 2