Regular variable regexp
I am trying to match the following example:
ZU2A ZS6D-9 ZT0ER-7 ZR6PJH-12
It is a combination of letters and numbers (alphanumeric). Here's an explanation:
- It always starts with capital (uppercase) Z
- Following is always only ONE (1) from R, S, T or U "[R | S | T | U]"
- Always follow only one (1) number "[0-9]"
- Constantly always with a minimum ONE (1) and possibly a maximum of three (3) uppercase letters such as [AZ] {1,3}
- Additionally follows "-" and minimum ONE (1) and maximum TWO (2) numbers
At the moment I have this:
Z[R|S|T|U][0-9][A-Z]{1,}(\-)?([0-9]{1,3})
But that doesn't sound like all samples.
EDIT: Here's an example of a complete line:
ZU0D>APT314,ZT1ER,WIDE1,ZS3PJ-2,ZR5STU-12*/V:/021414z2610.07S/02814.02Ek067/019/A=005475!w%<!
Any help would be appreciated.
thank
Danny
+3
source to share
1 answer
The main problem is that the entire extra part has to be surrounded by a single parenthesis, marked with ?
(= optional). In general, you want
Z[RSTU][0-9][A-Z]{1,3}(?:-[0-9]{1,2})?
A few additional notes:
- Within a character group, you can simply list the characters. So, for 2, you want either
[RSTU]
or(?:R|S|T|U)
. - The group in the form
(?:example)
instead(example)
prevents the subexpression from being returned as a match. It does not affect the matching of the inputs. - You don't need to hide
-
with a backslash outside the character class.
Here's an example test case script in Python :
import re
s = r'Z[RSTU][0-9][A-Z]{1,3}(?:-[0-9]{1,2})?'
rex = re.compile(s)
for test in ('ZU2A', 'ZS6D-9', 'ZT0ER-7', 'ZR6PJH-12'):
assert rex.match(test), test
long_test = 'ZU0D>APT314,ZT1ER,WIDE1,ZS3PJ-2,ZR5STU-12*/V:/021414z2610.07S/02814.02Ek067/019/A=005475!w%<!'
found = rex.findall(long_test)
assert found == ['ZU0D', 'ZT1ER', 'ZS3PJ-2', 'ZR5STU-12'], found
+5
source to share