Interview Question
Country: India
Warning: this method is pretty inefficient, but it should work.
for i in range (8):
for j in range(8):
if i != j:
for k in range (8):
if k == i:
string = string + r"\d"
elif k == j:
string = string + r"[a-zA-Z]"
else:
string = string + r"."
if i != 7 or j != 6:
string = string + r"|"
String will be your regular expression at the end
Essentially, this determines all possible positions of the two necessary characters, and creates a regular expression that takes them into account. I'd advise against this method if you can think of a better manner, but it should produce a working regex.
Edit: Another method using a couple of regexes, run
"^........$"
as your first regular expression.
After this, run the second expression which another user listed above:
".*\d+.*[a-zA-Z]+.*|.*[a-zA-Z]+.*\d+.*"
Still looking for a single expression that is more efficient than the previous one I listed, no luck so far.
^\d[A-Za-z]......|\d.[A-Za-z].....|\d..[A-Za-z]....|\d...[A-Za-z]...|\d....[A-Za-z]..|\d.....[A-Za-z].|\d......[A-Za-z]|[A-Za-z]\d......|.\d[A-Za-z].....|.\d.[A-Za-z]....|.\d..[A-Za-z]...|.\d...[A-Za-z]..|.\d....[A-Za-z].|.\d.....[A-Za-z]|[A-Za-z].\d.....|.[A-Za-z]\d.....|..\d[A-Za-z]....|..\d.[A-Za-z]...|..\d..[A-Za-z]..|..\d...[A-Za-z].|..\d....[A-Za-z]|[A-Za-z]..\d....|.[A-Za-z].\d....|..[A-Za-z]\d....|...\d[A-Za-z]...|...\d.[A-Za-z]..|...\d..[A-Za-z].|...\d...[A-Za-z]|[A-Za-z]...\d...|.[A-Za-z]..\d...|..[A-Za-z].\d...|...[A-Za-z]\d...|....\d[A-Za-z]..|....\d.[A-Za-z].|....\d..[A-Za-z]|[A-Za-z]....\d..|.[A-Za-z]...\d..|..[A-Za-z]..\d..|...[A-Za-z].\d..|....[A-Za-z]\d..|.....\d[A-Za-z].|.....\d.[A-Za-z]|[A-Za-z].....\d.|.[A-Za-z]....\d.|..[A-Za-z]...\d.|...[A-Za-z]..\d.|....[A-Za-z].\d.|.....[A-Za-z]\d.|......\d[A-Za-z]|[A-Za-z]......\d|.[A-Za-z].....\d|..[A-Za-z]....\d|...[A-Za-z]...\d|....[A-Za-z]..\d|.....[A-Za-z].\d|......[A-Za-z]\d$
This is "OR" of all permutations of "1a......". Yes, autogenerated, readonly, but it works
pattern = "expr1 OR expr2"
- PKT October 21, 2012exrp1 = [A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]
exrp2 = [0-9][A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9][A-Za-z]
Limitation: special chars can't be considered here