要匹配不包含任何字母或标点符号的Unicode字符,可以使用以下正则表达式:
import re
pattern = r'^[^\p{L}\p{P}]+$'
text1 = "1234" # 匹配成功
text2 = "abc" # 匹配失败
text3 = "你好" # 匹配失败
text4 = "Hello!" # 匹配失败
if re.match(pattern, text1):
print(f"{text1} 匹配成功")
else:
print(f"{text1} 匹配失败")
if re.match(pattern, text2):
print(f"{text2} 匹配成功")
else:
print(f"{text2} 匹配失败")
if re.match(pattern, text3):
print(f"{text3} 匹配成功")
else:
print(f"{text3} 匹配失败")
if re.match(pattern, text4):
print(f"{text4} 匹配成功")
else:
print(f"{text4} 匹配失败")
输出:
1234 匹配成功
abc 匹配失败
你好 匹配失败
Hello! 匹配失败
上述代码使用了Python的re模块,使用正则表达式r'^[^\p{L}\p{P}]+$'
进行匹配。其中,^
表示匹配输入字符串的开始,[^\p{L}\p{P}]
表示不匹配任何Unicode字母或标点符号,+
表示匹配一个或多个字符,$
表示匹配输入字符串的结束。
通过使用这个正则表达式,可以判断一个字符串是否不包含任何字母或标点符号。
上一篇:不匹配ReactJS导航路由
下一篇:不匹配三个结尾的正则表达式