可以使用以下正则表达式来解决这个问题:
^[a-zA-Z0-9]+([- ][a-zA-Z0-9]+)*$
这个正则表达式将允许以下内容:
下面是一个简单的Python代码示例,可以使用上述正则表达式来验证文本:
import re
def validate_text(text): pattern = r'^[a-zA-Z0-9]+([- ][a-zA-Z0-9]+)*$' if re.match(pattern, text): return True return False
print(validate_text("hello-world")) # True print(validate_text("hello_world")) # False print(validate_text("hello world")) # True print(validate_text(" a-b c d e-f ")) # True