I guess what you are looking for is this:
import re
pat = "^[^\']*\'[^\']*$"
print (re.match(pat, "aeh'3q4'bl;5hkj5l;ebj3'"))
print (re.match(pat, "aeh3q4'bl;5hkj5l;ebj3'"))
print (re.match(pat, "aeh3q4bl;5hkj5l;ebj3'"))
print (re.match(pat, "'"))
print (re.match(pat, ""))
Which gives output:
None
None
<_sre.SRE_Match object; span=(0, 21), match="aeh3q4bl;5hkj5l;ebj3'">
<_sre.SRE_Match object; span=(0, 1), match="'">
None
What "^[^\']*\'[^\']*$"
does?
^
matches the beginning of string
[^\']*
- *
matches 0 or more characters from set defined in []
. Here, we have a set negated using ^
. The set is defined as one character - '
, which is escaped so it looks \'
. Altogether, this group matches any number of any characters except '
\'
- matches one and only character '
$
- matches end of the string. Without it partial matches would be possible which could contain more '
characters. You can compare with above:
print (re.match("^[^\']*\'[^\']*", "aeh'3q4'bl;5hkj5l;ebj3'"))
<_sre.SRE_Match object; span=(0, 7), match="aeh'3q4">