import re
regex = re.compile(r"[a-zA-Z]+\d+|(?<=\d)[a-zA-Z]+", flags=re.MULTILINE)
test_str = ("新发布的三星盖乐世S9采用Exynos9810处理器,RAM为4GB,电池容量为3000mAh。\n"
"期望将S9,Exynos9810,GB,mAh提取出来\n\n"
"如何将一个句子中的英文词组或者英文+数字词组识别出来\n"
"新发布的三星盖乐世S9的特性如下:1.采用Exynos9810处理器;2.RAM为4GB;3.电池容量为3000mAh。\n"
"期望将S9,Exynos9810,GB,mAh提取出来,该用什么方法,希望得到各位的指点,谢谢。")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html