How about:
import re
s = "alpha.Customer[cus_Y4o9qMEZAugtnW] ..."
m = re.search(r"[([A-Za-z0-9_]+)]", s)
print m.group(1)
For me this prints:
cus_Y4o9qMEZAugtnW
Note that the call to re.search(...)
finds the first match to the regular expression, so it doesn't find the [card]
unless you repeat the search a second time.
Edit: The regular expression here is a python raw string literal, which basically means the backslashes are not treated as special characters and are passed through to the re.search()
method unchanged. The parts of the regular expression are:
[
matches a literal [
character
(
begins a new group
[A-Za-z0-9_]
is a character set matching any letter (capital or lower case), digit or underscore
+
matches the preceding element (the character set) one or more times.
)
ends the group
]
matches a literal ]
character
Edit: As D K has pointed out, the regular expression could be simplified to:
m = re.search(r"[(w+)]", s)
since the w
is a special sequence which means the same thing as [a-zA-Z0-9_]
depending on the re.LOCALE
and re.UNICODE
settings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…