我是正則運算式的新手,并試圖在 Python 中為以下字串獲取正則運算式,但仍然沒有運氣。只是想知道是否有人知道從以下位置獲取group1的最佳正則運算式:
test-50.group1-random01.ab.cd.website.com
基本上,我試圖在第一個點之后和第二個連字符之前獲取字串的一部分
uj5u.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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/479783.html
上一篇:洗掉文本中不必要的空格
