正则表达式在点之后和连字符之前获取部分字符串?

Regex to get part of the string after dot and before hyphen?

我是正则表达式的新手,并尝试在 Python 中获取以下字符串的正则表达式,但仍然没有成功。只是想知道是否有人知道从以下位置获得 group1 的最佳正则表达式:

test-50.group1-random01.ab.cd.website.com

基本上,我试图获取字符串的一部分 在第一个点之后和第二个连字符之前

你可以用 str.split

s = "test-50.group1-random01.ab.cd.website.com"
after_first_dot = s.split(".", maxsplit=1)[1]
before_hyphen   = after_first_dot.split("-")[0]
print(before_hyphen)  # group1

使用正则表达式,取点和连字符之间的内容

result = re.search(r"\.(.*?)-", s).group(1)
print(result)  # group1